Delimiter //
/**
 * Author:  armen
 * Created: 27 juil. 2017
 */

Drop Procedure if exists LOADER_EDMINDEX_ENTRIES_KEYWORDS_INSERT//
Create Procedure LOADER_EDMINDEX_ENTRIES_KEYWORDS_INSERT (
    In batchnum int
    , In attachedDocsLastId Int
)
Begin
	attachedDocInsert: Begin
		Insert Into ATTACHED_DOC_has_DF_KEYWORDS (
			ATTACHED_DOC_ID_ATTACHED_DOC, DF_KEYWORDS_ID_DF_KEYWORDS
		)
		Select 
			src.ID_DOCUMENTS_NEW + attachedDocsLastId
			, EDMKW.ID_DF_KEYWORDS 
		From LOADER_EDMINDEX src  
			Inner Join LOADER_STATUS lst  
				On src.LOADER_STATUS_ID_LOADER_STATUS = lst.ID_LOADER_STATUS  
			Inner Join LOADER_EDMINDEX_DOCUMENTS_HAS_KEYWORDS EDMKW
				On lst.ID_LOADER_STATUS = EDMKW.LOADER_STATUS_ID_LOADER_STATUS
		Where lst.LOADER_STATUS_ERRCOUNT = 0  
            And lst.LOADER_STATUS_BATCH_NUM =  batchnum
            And Coalesce(src.ID_DOCUMENTS_NEW, 0) <> 0 
			And EDMKW.ID_DF_KEYWORDS Is Not Null
		;
	End;
	
End //

Delimiter ;


