Delimiter //
/**
    In batchnum Int : num du lot, si 0 ou null traitement de l'ensembles des lignes
*/
Drop Procedure if exists LOADER_ACCOUNTS_INSERT//
Create Procedure LOADER_ACCOUNTS_INSERT (
    In batchnum int, 
    In lastId Int,
    In entitie Int
)
Begin

        Insert Into ACCOUNTS (
            ID_ACCOUNTS, ENTITIES_ID_ENTITIES, ACCOUNTS_PROCESSUS_SCAN_TYPE, ACCOUNTS_ACC_MNGT_LAST_LVL,
            ACCOUNTS_CREATED, ACCOUNTS_LAST_EVENT_UPDATE, ACCOUNTS_REF, ACCOUNTS_TYPE, 
            DF_CURRENCIES_ID_DF_CURRENCIES, DF_PRIORITY_ID_DF_PRIORITY, DF_SAND_STATE_ID_DF_SAND_STATE,
            DF_ACC_CATEG_ID_DF_ACC_CATEG, ENTITIES_AGENCIES_ID_ENTITIES_AGENCIES, DF_RATE_GRID_ID_DF_RATE_GRID,
            DF_DEBTS_TYPES_ID_DF_DEBTS_TYPES
        )  
        Select 
            src.ID_ACCOUNTS_NEW + lastId, entitie, 
            DF_SCAN_TYPE_ID_DF_SCAN_TYPE  , DF_MNGT_LVL_ID_DF_MNGT_LVL,
            src.ACCOUNTS_CREATED , src.ACCOUNTS_LAST_EVENT_UPDATE ,  src.ACCOUNTS_REF ,  src.DF_ACCOUNTS_TYPES_ID_DF_ACCOUNTS_TYPES, 
            src.DF_CURRENCIES_ID_DF_CURRENCIES , src.DF_PRIORITY_ID_DF_PRIORITY , src.DF_SAND_STATE_ID_DF_SAND_STATE ,
            DF_ACC_CATEG_ID_DF_ACC_CATEG, src.ENTITIES_AGENCIES_ID_ENTITIES_AGENCIES, src.DF_RATE_GRID_ID_DF_RATE_GRID,
            src.DF_DEBTS_TYPES_ID_DF_DEBTS_TYPES
        From  
            LOADER_ACCOUNTS src  
            Inner Join LOADER_STATUS lst  
                On src.LOADER_STATUS_ID_LOADER_STATUS = lst.ID_LOADER_STATUS  
        Where 1 = 1  
            And lst.LOADER_STATUS_ERRCOUNT = 0  
            And lst.LOADER_STATUS_BATCH_NUM =  batchnum
            and Coalesce(src.ID_ACCOUNTS_NEW, 0) <> 0 
        ;
End //

Delimiter ;





