Delimiter //
/**
 * Credit limit data insertion
 * @param int batchnum
 * @param int lastId
 * @author Ben
 */
Drop Procedure If Exists LOADER_CREDITLIMIT_CREDITLIMIT_INSERT//

Create Procedure LOADER_CREDITLIMIT_CREDITLIMIT_INSERT (
    In batchnum Int
    , In lastId Int
)
Begin
    set @lcci_batchnum = batchnum;
    set @lcci_lastId = lastId;
--     Insert Into CREDIT_LIMIT (
--         ID_CREDIT_LIMIT
--         , ACCOUNTS_ID_ACCOUNTS
--         , CREDIT_LIMIT_DATE
--         , CREDIT_LIMIT_AMOUNT
--         , CREDIT_LIMIT_VAL_DATE
--         , CREDIT_LIMIT_COMMENT
--     )  
--     Select 
--         src.ID_CREDIT_LIMIT_NEW + lastId
--         , src.ACCOUNTS_ID_ACCOUNTS
--         , src.CREDIT_LIMIT_DATE
--         , src.CREDIT_LIMIT_AMOUNT
--         , src.CREDIT_LIMIT_VAL_DATE
--         , src.CREDIT_LIMIT_COMMENT
--     From  
--         LOADER_CREDITLIMIT 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_CREDIT_LIMIT_NEW, 0) <> 0
--     ;
    Insert Into CREDIT_LIMIT (
        ID_CREDIT_LIMIT
        , ACCOUNTS_ID_ACCOUNTS
        , CREDIT_LIMIT_DATE
        , CREDIT_LIMIT_AMOUNT
        , CREDIT_LIMIT_VAL_DATE
        , CREDIT_LIMIT_COMMENT
    )  
    Select 
        src.ID_CREDIT_LIMIT_NEW + @lcci_lastId
        , src.ACCOUNTS_ID_ACCOUNTS
        , tbl1.CREDIT_LIMIT_DATE
        , tbl1.CREDIT_LIMIT_AMOUNT
        , tbl1.CREDIT_LIMIT_VAL_DATE
        , tbl1.CREDIT_LIMIT_COMMENT
    From  
        LOADER_CREDITLIMIT_ACCOUNTS src  
        Inner Join LOADER_CREDITLIMIT tbl1
            On src.LOADER_CREDITLIMIT_ID_LOADER_CREDITLIMIT = tbl1.ID_LOADER_CREDITLIMIT
        Inner Join LOADER_STATUS lst  
            On tbl1.LOADER_STATUS_ID_LOADER_STATUS = lst.ID_LOADER_STATUS  
    Where 1 = 1  
        And lst.LOADER_STATUS_ERRCOUNT = 0  
        And lst.LOADER_STATUS_BATCH_NUM =  @lcci_batchnum
        And Coalesce(src.ID_CREDIT_LIMIT_NEW, 0) <> 0
    ;

End //

Delimiter ;
