-- MOPDB
INSERT INTO CT_MRDS.A_TASK_HISTORY_TARGET (
	A_TASK_HISTORY_TARGET_KEY,
	A_TASK_HISTORY_KEY,
	TARGET_NAME,
	ROW_COUNT_APPLIED,
	ROW_COUNT_REJECTED,
	LOAD_SUCCESSFUL,
	SERVICE_NAME
)
SELECT 
  A_LOAD_HISTORY_TARGET_KEY AS A_TASK_HISTORY_TARGET_KEY,
  A_LOAD_SUBPROCESS_FK      AS A_TASK_HISTORY_KEY,
  TARGET_TABLE_NAME         AS TARGET_NAME,
  ROW_COUNT_APPLIED         AS ROW_COUNT_APPLIED,
  ROW_COUNT_REJECTED        AS ROW_COUNT_REJECTED,
  LOAD_SUCCESSFUL           AS LOAD_SUCCESSFUL,
  'MOPDB'                   AS SERVICE_NAME 
FROM
    CT_MOPDB.A_LOAD_HISTORY_TARGET
WHERE
    TARGET_TABLE_NAME IN (
    'LM_T_FC_ADH_ADJUSTMENTS',
    'LM_T_BALANCE_SHEET',
    'LM_T_FC_CSM_ADJUSTMENTS',
    'LM_T_STANDING_FACILITIES',
    'LM_T_CURRENT_ACCOUNTS',
    'LM_T_FORECAST',
    'LM_T_FC_QRE_ADJUSTMENTS',
    'LM_T_TTS'
    );


-- ODS
INSERT INTO CT_MRDS.A_TASK_HISTORY_TARGET (
	A_TASK_HISTORY_TARGET_KEY,
	A_TASK_HISTORY_KEY,
	TARGET_NAME,
	ROW_COUNT_APPLIED,
	ROW_COUNT_REJECTED,
	LOAD_SUCCESSFUL,
	SERVICE_NAME
)
SELECT 
  A_LOAD_HISTORY_TARGET_KEY           AS A_TASK_HISTORY_TARGET_KEY,
  A_ETL_LOAD_SET_FK                   AS A_TASK_HISTORY_KEY,
  CASE 
    WHEN TARGET_TABLE_NAME = 'OU_LM_ADHOC_ADJ_HEADER' THEN 'OU_LM_ADHOC_ADJUSTMENTS_HEADER'
    WHEN TARGET_TABLE_NAME = 'OU_LM_ADHOC_ADJ_ITEM' THEN 'OU_LM_ADHOC_ADJUSTMENTS_ITEM'
    WHEN TARGET_TABLE_NAME = 'OU_LM_ADHOC_ADJ_ITEM_HEADER' THEN 'OU_LM_ADHOC_ADJUSTMENTS_ITEM_HEADER'
    WHEN TARGET_TABLE_NAME = 'OU_LM_ADHOC_ADJ_ITEM_HEADER' THEN 'OU_LM_ADHOC_ADJUSTMENTS_ITEM_HEADER'
    WHEN TARGET_TABLE_NAME = 'OU_LM_CSM_ADJ_HEADER' THEN 'OU_LM_CSM_ADJUSTMENTS_HEADER'
    WHEN TARGET_TABLE_NAME = 'CSM_ADJ_HEADER' THEN 'OU_LM_CSM_ADJUSTMENTS_HEADER'
    WHEN TARGET_TABLE_NAME = 'OU_LM_CSM_ADJ_ITEM' THEN 'OU_LM_CSM_ADJUSTMENTS_ITEM'
    WHEN TARGET_TABLE_NAME = 'CSM_ADJ_ITEM' THEN 'OU_LM_CSM_ADJUSTMENTS_ITEM'
    WHEN TARGET_TABLE_NAME = 'OU_LM_CSM_ADJ_ITEM_HEADER' THEN 'OU_LM_CSM_ADJUSTMENTS_ITEM_HEADER'
    WHEN TARGET_TABLE_NAME = 'CSM_ADJ_ITEM_HEADER' THEN 'OU_LM_CSM_ADJUSTMENTS_ITEM_HEADER'
    WHEN TARGET_TABLE_NAME = 'OU_LM_STANDING_FACILITY' THEN 'OU_LM_STANDING_FACILITIES'
    WHEN TARGET_TABLE_NAME = 'STANDING_FACILITY' THEN 'OU_LM_STANDING_FACILITIES'
    WHEN TARGET_TABLE_NAME = 'OU_LM_STANDING_FACILITY_HEADER' THEN 'OU_LM_STANDING_FACILITIES_HEADER'
    WHEN TARGET_TABLE_NAME = 'STANDING_FACILITY_HEADER' THEN 'OU_LM_STANDING_FACILITIES_HEADER'
    WHEN TARGET_TABLE_NAME = 'OU_MRR_MRR_IND_CURR_ACC_HEADER' THEN 'OU_LM_CURRENT_ACCOUNTS_HEADER'
    WHEN TARGET_TABLE_NAME = 'OU_MRR_MRR_IND_CURR_ACC_ITEM' THEN 'OU_LM_CURRENT_ACCOUNTS_ITEM'
    WHEN TARGET_TABLE_NAME = 'OU_LM_QR_ADJ_HEADER' THEN 'OU_LM_QRE_ADJUSTMENTS_HEADER'
    WHEN TARGET_TABLE_NAME = 'QR_ADJ_HEADER' THEN 'OU_LM_QRE_ADJUSTMENTS_HEADER'
    WHEN TARGET_TABLE_NAME = 'OU_LM_QR_ADJ_ITEM' THEN 'OU_LM_QRE_ADJUSTMENTS_ITEM'
    WHEN TARGET_TABLE_NAME = 'QR_ADJ_ITEM' THEN 'OU_LM_QRE_ADJUSTMENTS_ITEM'
    WHEN TARGET_TABLE_NAME = 'OU_LM_QR_ADJ_ITEM_HEADER' THEN 'OU_LM_QRE_ADJUSTMENTS_ITEM_HEADER'
    WHEN TARGET_TABLE_NAME = 'QR_ADJ_ITEM_HEADER' THEN 'OU_LM_QRE_ADJUSTMENTS_ITEM_HEADER'
    ELSE TARGET_TABLE_NAME 
  END                                 AS TARGET_NAME,
  ROW_COUNT_APPLIED                   AS ROW_COUNT_APPLIED,
  ROW_COUNT_REJECTED                  AS ROW_COUNT_REJECTED,
  'Y'                                 AS LOAD_SUCCESSFUL,
  'ODS'                               AS SERVICE_NAME 
FROM
    CT_ODS.A_LOAD_HISTORY_TARGET 
WHERE
    TARGET_TABLE_NAME IN (
    'OU_LM_ADHOC_ADJ_HEADER',
    'OU_LM_ADHOC_ADJ_ITEM',
    'OU_LM_ADHOC_ADJ_ITEM_HEADER',
    'OU_LM_BALANCESHEET_HEADER',
    'OU_LM_BALANCESHEET_ITEM',
    'OU_LM_CSM_ADJ_HEADER',
    'OU_LM_CSM_ADJ_ITEM',
    'OU_LM_CSM_ADJ_ITEM_HEADER',
    'OU_LM_STANDING_FACILITY',
    'OU_LM_STANDING_FACILITY_HEADER',
    'OU_MRR_MRR_IND_CURR_ACC_HEADER',
    'OU_MRR_MRR_IND_CURR_ACC_ITEM',
    'OU_LM_FORECAST_HEADER',
    'OU_LM_FORECAST_ITEM',
    'OU_LM_QR_ADJ_HEADER',
    'OU_LM_QR_ADJ_ITEM',
    'OU_LM_QR_ADJ_ITEM_HEADER',
    'OU_LM_TTS_HEADER',
    'OU_LM_TTS_ITEM',
    'CSM_ADJ_HEADER',
    'CSM_ADJ_ITEM',
    'CSM_ADJ_ITEM_HEADER',
    'QR_ADJ_HEADER',
    'QR_ADJ_ITEM',
    'QR_ADJ_ITEM_HEADER',
    'STANDING_FACILITY',
    'STANDING_FACILITY_HEADER'
    );
    

COMMIT;	

