Delimiter //
/**
 * Provided entry doesn"t exist
 * 
 * -> Message d'erreur : "L'écriture #reference_externe du compte #ref_compte n'existe pas"
 * 
 * @param int batchnum
 */ 
Drop Procedure if Exists LOADER_EDMINDEX_NORMALIZE_CHECK_ENTRY_EXISTS//
Create Procedure LOADER_EDMINDEX_NORMALIZE_CHECK_ENTRY_EXISTS (
    In batchnum int
)
Begin
--         -- maj des erreurs
        Update
            LOADER_EDMINDEX LEDM
            Inner Join LOADER_STATUS LST
                On LST.ID_LOADER_STATUS = LEDM.LOADER_STATUS_ID_LOADER_STATUS
        Set 
            LST.LOADER_STATUS_ERRCOUNT = LST.LOADER_STATUS_ERRCOUNT + 1
                
            , LST.LOADER_STATUS_ERRMSGS = LOADER_CONCAT_ERR(LST.LOADER_STATUS_ERRMSGS, Concat('Référence de mouvement inexistant :', Coalesce(LEDM.ENTRIES_ACC_EXT_REF, LEDM.ENTRIES_REF) )) 
                
        Where
            LST.LOADER_STATUS_BATCH_NUM = batchnum
            And LEDM.LOADER_EDMINDEX_TYPE  = 'ENTRIES'
            And Coalesce(LEDM.ENTRIES_ACC_EXT_REF, LEDM.ENTRIES_REF, '') != ''
--             And LEDM.LOADER_STATUS_ID_LOADER_STATUS Not In (
            And Not Exists (
				Select
-- 					LEEHD.LOADER_STATUS_ID_LOADER_STATUS
					LEEHD.LOADER_EDMINDEX_ID_LOADER_EDMINDEX
				From LOADER_EDMINDEX_ENTRIES_HAS_DOCUMENTS LEEHD
				Where
-- 					LEDM.LOADER_STATUS_ID_LOADER_STATUS = LEEHD.LOADER_STATUS_ID_LOADER_STATUS
					LEDM.ID_LOADER_EDMINDEX = LEEHD.LOADER_EDMINDEX_ID_LOADER_EDMINDEX
                Limit 1
			)
        ;

End //
Delimiter ;