--
-- Properly set up LM Standing Facilities
--

-- 1. Create Template table CT_TEMPLTATE_TABLES.LM_STANDING_FACILITIES

CREATE TABLE CT_ET_TEMPLATES.LM_STANDING_FACILITIES 
   AS SELECT * FROM OU_LM.STANDING_FACILITIES WHERE 1=2;

-- 2. Create external tables

EXEC CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE('LM_STANDING_FACILITIES_INBOX','CT_ET_TEMPLATES.LM_STANDING_FACILITIES','INBOX/LM/STANDING_FACILITIES/STANDING_FACILITIES');
EXEC CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE('LM_STANDING_FACILITIES_ODS','CT_ET_TEMPLATES.LM_STANDING_FACILITIES','ODS/LM/STANDING_FACILITIES');
EXEC CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE('LM_STANDING_FACILITIES_ARCHIVE','CT_ET_TEMPLATES.LM_STANDING_FACILITIES','ARCHIVE/LM/STANDING_FACILITIES');

-- 3. Export table OU_LM.STANDING_FACILITIES to ARCHIVE

EXEC CT_MRDS.FILE_MANAGER.EXPORT_TABLE_DATA_BY_DATE('OU_LM','STANDING_FACILITIES','A_WORKFLOW_HISTORY_KEY','mrds_inbox_tst','ARCHIVE/LM/STANDING_FACILITIES');

-- 4. Rename OU_LM.STANDING_FACILITIES to OU_LM.STANDING_FACILITIES_LEGACY

ALTER TABLE OU_LM.STANDING_FACILITIES RENAME TO STANDING_FACILITIES_LEGACY;

-- 5. Create view OU_LM.STANDING_FACILITIES on top of ODS.LM_STANDING_FACILITIES_INBOX

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

