Delimiter //
/**
    In batchnum Int : num du lot, si 0 ou null traitement de l'ensembles des lignes
*/
Drop Procedure if exists LOADER_EDMINDEX_ACCOUNTS_HAS_DOCUMENTS_INSERT//
Create Procedure LOADER_EDMINDEX_ACCOUNTS_HAS_DOCUMENTS_INSERT (
    In batchnum int, 
    In lastId Int
)
Begin
	
	accountsAttachedDocInsert: begin
        Insert Into ACCOUNTS_has_ATTACHED_DOCUMENT(
            ATTACHED_DOC_ID_ATTACHED_DOC, ACCOUNTS_ID_ACCOUNTS
        )
        Select
			src.ID_DOCUMENTS_NEW + lastId
			, src.ACCOUNTS_ID_ACCOUNTS
        From
                LOADER_EDMINDEX 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_DOCUMENTS_NEW, 0) <> 0 
        ;
	end;
End //

Delimiter ;


