
Delimiter //
Drop Procedure If Exists LOADER_EDMINDEX_NORMALIZE_MAP_ACCOUNT_REF_PROVIDED//
Create Procedure LOADER_EDMINDEX_NORMALIZE_MAP_ACCOUNT_REF_PROVIDED(
    In batchnum Int
)
Begin
    set @entitie = GET_LOADER_INFO('ID_ENTITIES', batchnum);
        Update
            LOADER_EDMINDEX LEDM
            Inner Join LOADER_STATUS LST
                On LST.ID_LOADER_STATUS = LEDM.LOADER_STATUS_ID_LOADER_STATUS

            Inner Join ACCOUNTS ACC
                On ACC.ACCOUNTS_REF = LEDM.ACCOUNTS_REF
                And ACC.ENTITIES_ID_ENTITIES = @entitie
        Set
            LEDM.ACCOUNTS_ID_ACCOUNTS = ACC.ID_ACCOUNTS
        Where
            LST.LOADER_STATUS_BATCH_NUM = batchnum
        ;
        -- 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 compte inexistante :', LEDM.ACCOUNTS_REF )) 
                
        Where
            LST.LOADER_STATUS_BATCH_NUM = batchnum
            And Coalesce(LEDM.ACCOUNTS_REF, '') != ''
            And LEDM.ACCOUNTS_ID_ACCOUNTS Is Null
        ;

--     End If;

End//
Delimiter ;