Delimiter //
/**
 * Author:  armen
 * Created: 9 mars 2018
 */
Drop Procedure if exists LOADER_EDMINDEX_METADATA_UPDATE //
Create Procedure LOADER_EDMINDEX_METADATA_UPDATE (
    In batchnum int
)
Begin
    Set @lemdu_batchnum = batchnum;
	Update
        LOADER_STATUS sts
        Inner Join LOADER_EDMINDEX tbl1
            On sts.ID_LOADER_STATUS = tbl1.LOADER_STATUS_ID_LOADER_STATUS
        Inner Join LOADER_EDMINDEX_DOCUMENTS_METADATA src
            On tbl1.ID_LOADER_EDMINDEX = src.LOADER_EDMINDEX_ID_LOADER_EDMINDEX
        Inner Join DOCUMENTS_METADATA tar
            On tbl1.ID_DOCUMENTS = tar.DOCUMENTS_ID_DOCUMENTS
            And src.DOCUMENTS_METADATA_KEY = tar.DOCUMENTS_METADATA_KEY
    Set
        tar.DOCUMENTS_METADATA_INT = Coalesce(src.DOCUMENTS_METADATA_INT, tar.DOCUMENTS_METADATA_INT)
        , tar.DOCUMENTS_METADATA_EXTID = Coalesce(src.DOCUMENTS_METADATA_EXTID, tar.DOCUMENTS_METADATA_EXTID)
        , tar.DOCUMENTS_METADATA_DATE = Coalesce(src.DOCUMENTS_METADATA_DATE, tar.DOCUMENTS_METADATA_DATE)
        , tar.DOCUMENTS_METADATA_DECIMAL = Coalesce(src.DOCUMENTS_METADATA_DECIMAL, tar.DOCUMENTS_METADATA_DECIMAL)
        , tar.DOCUMENTS_METADATA_REF = Coalesce(src.DOCUMENTS_METADATA_REF, tar.DOCUMENTS_METADATA_REF)
        , tar.DOCUMENTS_METADATA_CONTENT = Coalesce(src.DOCUMENTS_METADATA_CONTENT, tar.DOCUMENTS_METADATA_CONTENT)
    Where
        sts.LOADER_STATUS_ERRCOUNT = 0
        And Trim(Coalesce(src.DOCUMENTS_METADATA_KEY, '')) <> ''
        And sts.LOADER_STATUS_BATCH_NUM = @lemdu_batchnum
    ;
End //

Delimiter ;

