Delimiter //
/**
    In batchnum Int : num du lot
*/
Drop Procedure if exists LOADER_RECLAIMS_INSERT//
Create Procedure LOADER_RECLAIMS_INSERT (
    In batchnum int
    , In lastId Int
)
Begin

        Insert Into RECLAIMS (
			ID_RECLAIMS, ACCOUNTS_ID_ACCOUNTS, MEMBERS_ID_MEMBERS, RECLAIMS_CREATED
			, RECLAIMS_ORIGINAL_AMOUNT, RECLAIMS_IS_PARTIAL, RECLAIMS_IS_TMP
        )  
        Select 

            src.NEW_ID_RECLAIMS + lastId, src.ACCOUNTS_ID_ACCOUNTS, src.MEMBERS_ID_MEMBERS, Coalesce(src.RECLAIMS_CREATED, curdate())
			, src.RECLAIMS_ORIGINAL_AMOUNT, 0, 0
        From  
            LOADER_RECLAIMS src  
            Inner Join LOADER_STATUS lst  
                On src.LOADER_STATUS_ID_LOADER_STATUS = lst.ID_LOADER_STATUS  
        Where 
            lst.LOADER_STATUS_ERRCOUNT = 0  
            And lst.LOADER_STATUS_BATCH_NUM =  batchnum
            and Coalesce(src.NEW_ID_RECLAIMS, 0) <> 0 
        ;
End //

Delimiter ;
