Delimiter //
/**
    In batchnum Int : num du lot, si 0 ou null traitement de l'ensembles des lignes
*/
Drop Procedure if exists LOADER_ENTRIES_HAS_ACCOUNTS_INSERT//
Create Procedure LOADER_ENTRIES_HAS_ACCOUNTS_INSERT (
    In batchnum int, 
    In lastId Int
)
Begin
        Insert Into ENTRIES_has_ACCOUNTS (
            ENTRIES_ID_ENTRIES, ACCOUNTS_ID_ACCOUNTS, ENTRIES_ACC_EXT_REF, ENTRIES_PERMANENT_ATTACHEMENT
        )
        Select
            TBL_1.ID_ENTRIES_NEW + lastId,
            src.ACCOUNTS_ID_ACCOUNTS, src.ENTRIES_ACC_EXT_REF, src.ENTRIES_PERMANENT_ATTACHEMENT
        From
                LOADER_ENTRIES_ENTRIES_has_ACCOUNTS src  
				Inner Join LOADER_ENTRIES TBL_1
					On src.LOADER_ENTRIES_ID_LOADER_ENTRIES = TBL_1.ID_LOADER_ENTRIES
                Inner Join LOADER_STATUS lst  
                    On TBL_1.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(TBL_1.ID_ENTRIES_NEW, 0) <> 0 
        ;

End //

Delimiter ;


