Delimiter //
Drop Procedure If Exists LOADER_ENTRIES_NORMALIZE_MAP_CONTACT_REF_PROVIDED//
Create Procedure LOADER_ENTRIES_NORMALIZE_MAP_CONTACT_REF_PROVIDED(
    In batchnum Int
)
Begin
    set @lenmcrp_batchnum = batchnum;
#     set @lenmcrp_entitie = GET_LOADER_INFO('ID_ENTITIES', @lenmcrp_batchnum);
    Update
        LOADER_STATUS lst
        Inner Join LOADER_ENTRIES le
            On lst.ID_LOADER_STATUS = le.LOADER_STATUS_ID_LOADER_STATUS
        Left Join CONTACTS ctc
            On le.CONTACTS_REF = ctc.CONTACTS_REF
            And ctc.CONTACTS_TRASHED = 0
    Set
        le.CONTACTS_ID_CONTACTS = ctc.ID_CONTACTS
        , lst.LOADER_STATUS_WARNCOUNT = Coalesce(lst.LOADER_STATUS_WARNCOUNT, 0) + Case When ctc.ID_CONTACTS Is Null Then 1 Else 0 End
        , lst.LOADER_STATUS_WARNMSGS =  Case When ctc.ID_CONTACTS Is Null
                                            Then
                                                 LOADER_CONCAT_ERR(lst.LOADER_STATUS_WARNMSGS, Concat('Contact inexistant : "', le.CONTACTS_REF, '"'))
                                            Else
                                                lst.LOADER_STATUS_WARNMSGS
                                        End
    Where
        lst.LOADER_STATUS_BATCH_NUM = @lenmcrp_batchnum
        And Coalesce(le.CONTACTS_REF, '') <> ''
    ;

End//
Delimiter ;