--
-- Properly set up MDP Standing Facilities
--

-- 1. Create Template table CT_TEMPLTATE_TABLES.MDP_BBG_EQUITY_OT

CREATE TABLE CT_ET_TEMPLATES.MDP_BBG_EQUITY_OT
   AS SELECT * FROM OU_MDP.BBG_EQUITY_OT WHERE 1=2;

-- 2. Create external tables

EXEC CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE('MDP_BBG_EQUITY_OT_INBOX','CT_ET_TEMPLATES.MDP_BBG_EQUITY_OT','INBOX/MDP/BBG_EQUITY_OT/BBG_EQUITY_OT');
EXEC CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE('MDP_BBG_EQUITY_OT_ODS','CT_ET_TEMPLATES.MDP_BBG_EQUITY_OT','ODS/MDP/BBG_EQUITY_OT');
EXEC CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE('MDP_BBG_EQUITY_OT_ARCHIVE','CT_ET_TEMPLATES.MDP_BBG_EQUITY_OT','ARCHIVE/MDP/BBG_EQUITY_OT');

-- 3. Export table OU_MDP.BBG_EQUITY_OT to ARCHIVE

EXEC CT_MRDS.FILE_MANAGER.EXPORT_TABLE_DATA_BY_DATE('OU_MDP','BBG_EQUITY_OT','A_WORKFLOW_HISTORY_KEY','mrds_inbox_tst','ARCHIVE/MDP/BBG_EQUITY_OT');

-- 4. Rename OU_MDP.BBG_EQUITY_OT to OU_MDP.BBG_EQUITY_OT_LEGACY

ALTER TABLE OU_MDP.BBG_EQUITY_OT RENAME TO BBG_EQUITY_OT_LEGACY;

-- 5. Create view OU_MDP.BBG_EQUITY_OT on top of ODS.MDP_BBG_EQUITY_OT_INBOX

REVOKE SELECT ON ods.lm_standing_facilities_inbox from OU_MDP;
GRANT SELECT ON ods.lm_standing_facilities_ods TO OU_MDP;
create or replace view OU_MDP.BBG_EQUITY_OT as select * from  ods.lm_standing_facilities_ods;

