Delimiter //
/**
 * Author:  armen
 * Created: 8 mars 2018
 */

Drop Procedure If Exists LOADER_EDMINDEX_NORMALIZE_MAP_METADATA_PROVIDED_DOINSERT//
Create Procedure LOADER_EDMINDEX_NORMALIZE_MAP_METADATA_PROVIDED_DOINSERT(
    In batchnum Int
    , In metadataNo Int
)
Begin

        set @lencmp_batchnum = batchnum;
		set @lencmp_metadataNo = metadataNo;
        set @lencmp_query  = concat(
                ' Insert Into LOADER_EDMINDEX_DOCUMENTS_METADATA ( '
                , '    LOADER_EDMINDEX_ID_LOADER_EDMINDEX '
                , '    , LOADER_STATUS_ID_LOADER_STATUS '
--                 , '    , LOADER_EDMINDEX_ID_DOCUMENTS '
--                 , '    , LOADER_EDMINDEX_ID_DOCUMENTS_NEW '
                , '    , DOCUMENTS_METADATA_KEY '
                , '    , DOCUMENTS_METADATA_INT '
                , '    , DOCUMENTS_METADATA_EXTID '
                , '    , DOCUMENTS_METADATA_DATE '
                , '    , DOCUMENTS_METADATA_DECIMAL '
                , '    , DOCUMENTS_METADATA_REF '
                , '    , DOCUMENTS_METADATA_CONTENT '
                , ' ) '
                , ' Select '
                , '    led.ID_LOADER_EDMINDEX '
                , '    , lst.ID_LOADER_STATUS '
--                 , '    , led.ID_DOCUMENTS '
--                 , '    , led.ID_DOCUMENTS_NEW '
                , '    , led.DOCUMENTS_METADATA_KEY', @lencmp_metadataNo, ' '
                , '    , led.DOCUMENTS_METADATA_INT', @lencmp_metadataNo, ' '
                , '    , led.DOCUMENTS_METADATA_EXTID', @lencmp_metadataNo, ' '
                , '    , led.DOCUMENTS_METADATA_DATE', @lencmp_metadataNo, ' '
                , '    , led.DOCUMENTS_METADATA_DECIMAL', @lencmp_metadataNo, ' '
                , '    , led.DOCUMENTS_METADATA_REF', @lencmp_metadataNo, ' '
                , '    , led.DOCUMENTS_METADATA_CONTENT', @lencmp_metadataNo, ' '
                , ' From '
                , '    LOADER_STATUS lst '
                , '    Inner Join LOADER_EDMINDEX led '
                , '        on lst.ID_LOADER_STATUS = led.LOADER_STATUS_ID_LOADER_STATUS '
                , ' Where '
                , '    lst.LOADER_STATUS_ERRCOUNT = 0 '
                , '    And lst.LOADER_STATUS_BATCH_NUM = ? '
                    -- test dans la procédure LOADER_EDMINDEX_NORMALIZE_CHECK_METADATA_EMPTY
--                 , '    And trim(Coalesce(led.DOCUMENTS_METADATA_KEY1, '''')) <> '''' '
            )

        ;

		Prepare lencmp_stmt From @lencmp_Query;
		execute lencmp_stmt using @lencmp_batchnum	;	
		Deallocate prepare lencmp_stmt;

End//
Delimiter ;