Delimiter //
/**
    In batchnum Int : num du lot, si 0 ou null traitement de l'ensembles des lignes
    
*/

Drop Procedure if exists LOADER_CONTACTS_BUSINESS_UPDATE//

Create Procedure LOADER_CONTACTS_BUSINESS_UPDATE (
    In batchnum int
)
Begin
        Update 
            CONTACTS_BUSINESS tar
            Inner Join LOADER_CONTACTS src  
                On tar.CONTACTS_ID_CONTACTS = src.ID_CONTACTS
            Inner Join LOADER_STATUS lst  
                On src.LOADER_STATUS_ID_LOADER_STATUS = lst.ID_LOADER_STATUS  
--             Inner Join CONTACTS_INDIVIDUAL TBL_1
--                 On src.ID_CONTACTS = TBL_1.CONTACTS_ID_CONTACTS

        Set

            tar.CONTACTS_BUSINESS_SIRET = Coalesce(src.CONTACTS_BUSINESS_SIRET, tar.CONTACTS_BUSINESS_SIRET), 
            tar.CONTACTS_BUSINESS_LAST_UPDATE = Coalesce(src.CONTACTS_LAST_UPDATE, tar.CONTACTS_BUSINESS_LAST_UPDATE), 
            tar.CONTACTS_BUSINESS_CREATED_DATE = Coalesce(src.CONTACTS_LAST_UPDATE, tar.CONTACTS_BUSINESS_CREATED_DATE), 
            tar.CONTACTS_BUSINESS_COMPANY_NAME = Coalesce(src.CONTACTS_BUSINESS_COMPANY_NAME, tar.CONTACTS_BUSINESS_COMPANY_NAME),
            tar.CONTACTS_BUSINESS_BRAND_NAME = Coalesce(src.CONTACTS_BUSINESS_BRAND_NAME, tar.CONTACTS_BUSINESS_BRAND_NAME), 
--             tar.CONTACT_INDIV_ID_CONTACT_INDIV = Coalesce(TBL_1.ID_CONTACTS_INDIVIDUAL, tar.CONTACT_INDIV_ID_CONTACT_INDIV),
            tar.DF_FORMS_ID_DF_FORMS = Coalesce(src.DF_FORMS_ID_DF_FORMS, tar.DF_FORMS_ID_DF_FORMS)
        Where 1 = 1  
            And lst.LOADER_STATUS_ERRCOUNT = 0  
            And lst.LOADER_STATUS_BATCH_NUM =  batchnum
--             And Coalesce(src.CONTACTS_BUSINESS_COMPANY_NAME, '') <> ''
        ;
End //

Delimiter ;