Delimiter //
/**
 * Author:  armen
 * Created: 08 octobre 2021
 */
Drop Procedure if exists LOADER_ACCOUNTS_COPY_ACCOUNTSSTATUS_UPDATE//
Create Procedure LOADER_ACCOUNTS_COPY_ACCOUNTSSTATUS_UPDATE (
    In batchnum int
)
Begin
        Update 
            ACCOUNTS_STATUS trgt 
            Inner Join LOADER_ACCOUNTS src 
                On trgt.ACCOUNTS_ID_ACCOUNTS = src.ID_ACCOUNTS 
            Inner Join LOADER_STATUS lst 
                On src.LOADER_STATUS_ID_LOADER_STATUS = lst.ID_LOADER_STATUS 
        Set 

            trgt.DF_ACCOUNTS_STATUS_ID_DF_ACCOUNTS_STATUS = Coalesce(src.DF_ACCOUNTS_STATUS_ID_DF_ACCOUNTS_STATUS, trgt.DF_ACCOUNTS_STATUS_ID_DF_ACCOUNTS_STATUS)
            , trgt.MEMBERS_ID_MEMBERS = 1
            , trgt.ACCOUNTS_STATUS_VAL_DATE = Coalesce(src.ACCOUNTS_STATUS_VAL_DATE , trgt.ACCOUNTS_STATUS_VAL_DATE)
            , trgt.ACCOUNTS_STATUS_COMMENT = Coalesce(src.ACCOUNTS_STATUS_COMMENT , trgt.ACCOUNTS_STATUS_COMMENT)
            , trgt.ACCOUNTS_STATUS_UPDATE_SRC = 2
            , trgt.ACCOUNTS_STATUS_DATE_UPDT_IMPORT = curdate()

       Where 1 = 1 
            And lst.LOADER_STATUS_ERRCOUNT = 0 
            And lst.LOADER_STATUS_BATCH_NUM =  batchnum
            -- And and Coalesce(src.ID_ACCOUNTS, 0) <> 0 -- implicite
        ;
End //

Delimiter ;
