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

Drop Procedure if exists LOADER_CONTACTS_INDIVIDUAL_UPDATE//

Create Procedure LOADER_CONTACTS_INDIVIDUAL_UPDATE (
    In batchnum int
)
Begin
        update 
            CONTACTS_INDIVIDUAL 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  

        Set
            tar.DF_TITLES_ID_DF_TITLES = Coalesce(src.DF_TITLES_ID_DF_TITLES, tar.DF_TITLES_ID_DF_TITLES),
            tar.DF_FUNCTIONS_ID_DF_FUNCTIONS = Coalesce(src.DF_FUNCTIONS_ID_DF_FUNCTIONS, tar.DF_FUNCTIONS_ID_DF_FUNCTIONS),
            tar.CONTACTS_INDIVIDUAL_FIRST_NAME = Coalesce(src.CONTACTS_INDIVIDUAL_FIRST_NAME, tar.CONTACTS_INDIVIDUAL_FIRST_NAME), 
            tar.CONTACTS_INDIVIDUAL_LAST_NAME = Coalesce(src.CONTACTS_INDIVIDUAL_LAST_NAME, tar.CONTACTS_INDIVIDUAL_LAST_NAME),
            tar.CONTACTS_INDIVIDUAL_BIRTH_NAME = Coalesce(src.CONTACTS_INDIVIDUAL_BIRTH_NAME, tar.CONTACTS_INDIVIDUAL_BIRTH_NAME), 
            -- tar.CONTACTS_INDIVIDUAL_BIRTH_DAY = , 
            tar.CONTACTS_INDIVIDUAL_LAST_UPDAT = Coalesce(src.CONTACTS_LAST_UPDATE, tar.CONTACTS_INDIVIDUAL_LAST_UPDAT)

        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 ;