
Delimiter //
Drop Procedure If Exists LOADER_SUMMARIES_NORMALIZE_MAP_ENTRIES//
Create Procedure LOADER_SUMMARIES_NORMALIZE_MAP_ENTRIES(
    In batchnum Int
)
Begin

    Update
        LOADER_STATUS STS
        Inner Join LOADER_SUMMARIES LRD
            On LRD.LOADER_STATUS_ID_LOADER_STATUS = STS.ID_LOADER_STATUS
		Inner Join ENTRIES_has_ACCOUNTS EHA
			On LRD.ACCOUNTS_ID_ACCOUNTS = EHA.ACCOUNTS_ID_ACCOUNTS
			And LRD.ENTRIES_ACC_EXT_REF = EHA.ENTRIES_ACC_EXT_REF
    Set
        LRD.ENTRIES_ID_ENTRIES = EHA.ENTRIES_ID_ENTRIES
    Where
        STS.LOADER_STATUS_BATCH_NUM = batchnum
    ;


    -- maj des erreur

	call LOADER_BUILD_SUMMARIES_CHECK_BASE_QUERY (
				'ENTRIES_ACC_EXT_REF', batchnum, 'Entries not found', '', 0, @query
			)
		;
	set @query = Concat(@query
					, ' And Coalesce(ENTRIES_ACC_EXT_REF, '''') <> '''' '
					, ' And ENTRIES_ID_ENTRIES Is Null'
				)
			;

	PREPARE stmt FROM @query;
	execute stmt			
		;
	Deallocate prepare stmt;

End//
Delimiter ;