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

Drop Procedure if exists LOADER_ENTRIES_UPDATE//

Create Procedure LOADER_ENTRIES_UPDATE (
    In batchnum int
)
Begin
    Update ENTRIES trgt  
        Inner Join LOADER_ENTRIES src  
            On trgt.ID_ENTRIES = src.ID_ENTRIES
        Inner Join LOADER_STATUS lst  
            On src.LOADER_STATUS_ID_LOADER_STATUS = lst.ID_LOADER_STATUS

    Set

    trgt.DF_MATCH_STAT_ID_DF_MATCH_STAT = Coalesce(src.DF_MATCH_STAT_ID_DF_MATCH_STAT, trgt.DF_MATCH_STAT_ID_DF_MATCH_STAT),
    trgt.DF_SAND_STATE_ID_DF_SAND_STATE = Coalesce(src.DF_SAND_STATE_ID_DF_SAND_STATE, trgt.DF_SAND_STATE_ID_DF_SAND_STATE),
    trgt.ID_DF_PAY_SCHED_STATUS = Coalesce(src.DF_PAY_SCHED_STATUS_ID_DF_PAY_SCHED_STATUS, trgt.ID_DF_PAY_SCHED_STATUS),
    trgt.DF_CURRENCIES_ID_DF_CURRENCIES = Coalesce(src.DF_CURRENCIES_ID_DF_CURRENCIES, trgt.DF_CURRENCIES_ID_DF_CURRENCIES),
    trgt.DF_ENTR_TYPE_ID_DF_ENTR_TYPE = Coalesce(src.DF_ENTR_TYPE_ID_DF_ENTR_TYPE, trgt.DF_ENTR_TYPE_ID_DF_ENTR_TYPE),
    trgt.DF_PAY_TYPE_ID_DF_PAY_TYPE = Coalesce(src.DF_PAY_TYPE_ID_DF_PAY_TYPE, trgt.DF_PAY_TYPE_ID_DF_PAY_TYPE),
    trgt.ENTRIES_ACCOUTING_ACCOUNTS = Coalesce(src.ENTRIES_ACCOUTING_ACCOUNTS, trgt.ENTRIES_ACCOUTING_ACCOUNTS),
    trgt.ENTRIES_DATE = Coalesce(src.ENTRIES_DATE, trgt.ENTRIES_DATE),
    trgt.ENTRIES_DUE_DATE = Coalesce(src.ENTRIES_DUE_DATE, trgt.ENTRIES_DUE_DATE),
    trgt.ENTRIES_LABEL = Coalesce(src.ENTRIES_LABEL, trgt.ENTRIES_LABEL),
    trgt.ENTRIES_PURCHASE_ORDER = Coalesce(src.ENTRIES_PURCHASE_ORDER, trgt.ENTRIES_PURCHASE_ORDER),
    trgt.ENTRIES_REF = Coalesce(src.ENTRIES_REF, trgt.ENTRIES_REF) ,
    trgt.ENTRIES_DEBIT = Coalesce(src.ENTRIES_DEBIT, trgt.ENTRIES_DEBIT, 0),
    trgt.ENTRIES_CREDIT = Coalesce(src.ENTRIES_CREDIT, trgt.ENTRIES_CREDIT, 0),
    trgt.ENTRIES_ADMIN_DEBIT = Coalesce(src.ENTRIES_ADMIN_DEBIT, trgt.ENTRIES_ADMIN_DEBIT, 0),
    trgt.ENTRIES_ADMIN_CREDIT = Coalesce(src.ENTRIES_ADMIN_CREDIT, trgt.ENTRIES_ADMIN_CREDIT, 0),
    trgt.ENTRIES_ENT_DEBIT = Coalesce(src.ENTRIES_ENT_DEBIT, trgt.ENTRIES_ENT_DEBIT, 0),
    trgt.ENTRIES_ENT_CREDIT = Coalesce(src.ENTRIES_ENT_CREDIT, trgt.ENTRIES_ENT_CREDIT, 0),
    trgt.ENTRIES_ENT_BALANCE = Coalesce(src.ENTRIES_ENT_BALANCE, trgt.ENTRIES_ENT_BALANCE, 0),
    trgt.DF_ENTR_STT_ID_DF_ENTR_STT = Coalesce(src.DF_ENTR_STT_ID_DF_ENTR_STT, trgt.DF_ENTR_STT_ID_DF_ENTR_STT),
    trgt.ENTRIES_PROMISE = Coalesce(src.ENTRIES_PROMISE, trgt.ENTRIES_PROMISE, 0),
    trgt.ENTRIES_PROMISE_DATE = Coalesce(src.ENTRIES_PROMISE_DATE, trgt.ENTRIES_PROMISE_DATE),
    trgt.CONTACTS_ID_CONTACTS = Coalesce(src.CONTACTS_ID_CONTACTS, trgt.CONTACTS_ID_CONTACTS)

    Where 1 = 1  
        And lst.LOADER_STATUS_ERRCOUNT = 0  
        And lst.LOADER_STATUS_BATCH_NUM =  batchnum
    ;
End //

Delimiter ;