Delimiter //
/**
    In batchnum Int : num du lot, si 0 ou null traitement de l'ensembles des lignes
*/
Drop Procedure if exists LOADER_EDMINDEX_ENTRIES_HAS_DOCUMENTS_INSERT//
Create Procedure LOADER_EDMINDEX_ENTRIES_HAS_DOCUMENTS_INSERT (
    In batchnum int, 
    In lastId Int
)
Begin
	
	entriesAttachedDocInsert: begin
        Insert Into ENTRIES_has_ATTACHED_DOCUMENTS(
            ATTACHED_DOC_ID_ATTACHED_DOC
			, ENTRIES_ID_ENTRIES
        )
        Select
			src.ID_DOCUMENTS_NEW + lastId
			, LEEHD.ENTRIES_ID_ENTRIES
        From
                LOADER_EDMINDEX src  
                Inner Join LOADER_STATUS lst  
                    On src.LOADER_STATUS_ID_LOADER_STATUS = lst.ID_LOADER_STATUS  
				Inner Join LOADER_EDMINDEX_ENTRIES_HAS_DOCUMENTS LEEHD
					On lst.ID_LOADER_STATUS  = LEEHD.LOADER_STATUS_ID_LOADER_STATUS
        Where 1 = 1  
            And lst.LOADER_STATUS_ERRCOUNT = 0  
            And lst.LOADER_STATUS_BATCH_NUM =  batchnum
			And src.LOADER_EDMINDEX_TYPE = 'ENTRIES'
            And Coalesce(src.ID_DOCUMENTS_NEW, 0) <> 0 
        ;
	end;
End //

Delimiter ;


