--MARS-362
set verify off
SET serveroutput ON
--SET TIMING ON
whenever sqlerror exit sql.sqlcode rollback;
column owner format a15
column object_name format a30
column spoolname new_value spoolname 
select 'install_package_RQSDProcess_MARS362_patch_' || name || '_' ||to_char(sysdate,'YYYYMMDD_HH24MISS')||'.log' spoolname from v$pdbs;
spool '&spoolname'
prompt ##### started at time #####
select systimestamp from dual;
prompt ##### database name #####
select name pdb_name from v$pdbs;
--set echo on
--=============================================================================================================================
-- start install --MARS-362 PATCH DEVO THINGS 
--=============================================================================================================================


UPDATE CT_MRDS.A_DEVO_REPLICA_MGMT_MOPDB SET TABLE_ALIAS = TABLE_NAME || '_EXT';
commit;

UPDATE CT_MRDS.A_DEVO_REPLICA_MGMT_RAR SET TABLE_ALIAS = TABLE_ALIAS || '_EXT';
commit;

UPDATE CT_MRDS.A_DEVO_REPLICA_MGMT_RQSD SET TABLE_ALIAS = TABLE_NAME || '_EXT';
commit;

INSERT INTO CT_MRDS.A_DEVO_REPLICA_MGMT_MOPDB (OWNER, TABLE_NAME, TABLE_ALIAS, SAVE_MODE, PARTITION_COLUMN, MAX_THREADS) 
SELECT OWNER, TABLE_NAME || '_COPY', TABLE_NAME, 'OVERWRITE', 'ROWID', 1 FROM CT_MRDS.A_DEVO_REPLICA_MGMT_MOPDB;
commit;

INSERT INTO CT_MRDS.A_DEVO_REPLICA_MGMT_RAR (OWNER, TABLE_NAME, TABLE_ALIAS, SAVE_MODE, PARTITION_COLUMN, MAX_THREADS) 
SELECT OWNER, TABLE_NAME || '_COPY',  SUBSTR(TABLE_ALIAS, 0,LENGTH(TABLE_ALIAS)-4), 'OVERWRITE', 'ROWID', 1 FROM CT_MRDS.A_DEVO_REPLICA_MGMT_RAR;
commit;

INSERT INTO CT_MRDS.A_DEVO_REPLICA_MGMT_RQSD (OWNER, TABLE_NAME, TABLE_ALIAS, SAVE_MODE, PARTITION_COLUMN, MAX_THREADS) 
SELECT OWNER, TABLE_NAME || '_COPY', TABLE_NAME, 'OVERWRITE', 'ROWID', 1 FROM CT_MRDS.A_DEVO_REPLICA_MGMT_RQSD;
commit;

INSERT INTO CT_MOPDB.MOPDB_METADATA_INVENTORY (A_RAR_METADATA_INVENTORY_KEY, A_VALID_FROM, A_VALID_TO, OWNER,TABLE_NAME, COLUMN_NAME, DATA_TYPE, CHAR_LENGTH, DATA_PRECISION, DATA_SCALE, COLUMN_ID, DATA_DESCRIPTION, LIST_OF_SOURCES, RAR3_TYPE_OF_ACCESS, TMS, C2D_EA, C2D_ELA, C2D_MPEC, C2D_UC, CEPH, CSDB_REFERENCE, CSDB_RATINGS, FXCD, MDP_BBG, MDP_REU, RIAD_CL, RIAD_OU, SDW_ESTR, SDW_FX, TOP, RAR, RTM, RAR_DATABASE, LED, MDP_CMA, LM_SF, LM_BALANCESHEET, LM_MRR, LM_FORECAST, LM_ADJ, LM_TTS)
SELECT DISTINCT A_RAR_METADATA_INVENTORY_KEY + 10000000, A_VALID_FROM, A_VALID_TO, OWNER,TABLE_NAME || '_COPY', COLUMN_NAME, DATA_TYPE, CHAR_LENGTH, DATA_PRECISION, DATA_SCALE, COLUMN_ID, DATA_DESCRIPTION, LIST_OF_SOURCES, RAR3_TYPE_OF_ACCESS, TMS, C2D_EA, C2D_ELA, C2D_MPEC, C2D_UC, CEPH, CSDB_REFERENCE, CSDB_RATINGS, FXCD, MDP_BBG, MDP_REU, RIAD_CL, RIAD_OU, SDW_ESTR, SDW_FX, TOP, RAR, RTM, RAR_DATABASE, LED, MDP_CMA, LM_SF, LM_BALANCESHEET, LM_MRR, LM_FORECAST, LM_ADJ, LM_TTS
FROM CT_MOPDB.MOPDB_METADATA_INVENTORY
WHERE A_VALID_TO > SYSDATE 
AND (OWNER, TABLE_NAME) IN (SELECT OWNER, TABLE_NAME FROM CT_MRDS.A_DEVO_REPLICA_MGMT_MOPDB);
commit;

INSERT INTO DW_RAR.NH_METADATA_INVENTORY (A_RAR_METADATA_INVENTORY_KEY, A_VALID_FROM, A_VALID_TO, OWNER,TABLE_NAME, COLUMN_NAME, DATA_TYPE, CHAR_LENGTH, DATA_PRECISION, DATA_SCALE, COLUMN_ID, DATA_DESCRIPTION, LIST_OF_SOURCES, RAR3_TYPE_OF_ACCESS, TMS, C2D_EA, C2D_ELA, C2D_MPEC, C2D_UC, CEPH, LM,CSDB_REFERENCE,CSDB_RATINGS,FXCD,MDP_BBG,MDP_REU,RIAD_CL,RIAD_OU,SDW_ESTR,SDW_FX,TOP,RAR,RTM,LED,MDP_CMA,PART_OF_UID,RIAD_GR,MORA_LBA, RAR_DATABASE)
SELECT DISTINCT A_RAR_METADATA_INVENTORY_KEY + 10000000, A_VALID_FROM, A_VALID_TO, OWNER,TABLE_NAME || '_COPY', COLUMN_NAME, DATA_TYPE, CHAR_LENGTH, DATA_PRECISION, DATA_SCALE, COLUMN_ID, DATA_DESCRIPTION, LIST_OF_SOURCES, RAR3_TYPE_OF_ACCESS,TMS, C2D_EA, C2D_ELA, C2D_MPEC, C2D_UC, CEPH, LM,CSDB_REFERENCE,CSDB_RATINGS,FXCD,MDP_BBG,MDP_REU,RIAD_CL,RIAD_OU,SDW_ESTR,SDW_FX,TOP,RAR,RTM,LED,MDP_CMA,PART_OF_UID,RIAD_GR,MORA_LBA, RAR_DATABASE
FROM CORR_RAR.NH_METADATA_INVENTORY
WHERE A_VALID_TO > SYSDATE 
AND (OWNER, TABLE_NAME) IN (SELECT OWNER, TABLE_NAME FROM CT_MRDS.A_DEVO_REPLICA_MGMT_RAR);
commit;

INSERT INTO CT_MRDS.A_DEVO_METADATA_INVENTORY (A_VALID_FROM, A_VALID_TO, OWNER,TABLE_NAME, COLUMN_NAME, DATA_TYPE, CHAR_LENGTH, DATA_PRECISION, DATA_SCALE, COLUMN_ID, DATA_DESCRIPTION, LIST_OF_SOURCES, RAR3_TYPE_OF_ACCESS, SERVICE_NAME)
SELECT DISTINCT A_VALID_FROM, A_VALID_TO, OWNER,TABLE_NAME || '_COPY', COLUMN_NAME, DATA_TYPE, CHAR_LENGTH, DATA_PRECISION, DATA_SCALE, COLUMN_ID, DATA_DESCRIPTION, LIST_OF_SOURCES, RAR3_TYPE_OF_ACCESS, SERVICE_NAME
FROM CT_MRDS.A_DEVO_METADATA_INVENTORY
WHERE A_VALID_TO > SYSDATE
AND (OWNER, TABLE_NAME) IN (SELECT OWNER, TABLE_NAME FROM CT_MRDS.A_DEVO_REPLICA_MGMT_RQSD);
commit;