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

-- 1. Create Template table CT_TEMPLTATE_TABLES.LM_STANDING_FACILITIES

CREATE TABLE CT_ET_TEMPLATES.LM_STANDING_FACILITIES_HEADER
   AS SELECT * FROM OU_LM.STANDING_FACILITIES_HEADER WHERE 1=2;

-- 2. Create external tables

EXEC CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE('LM_STANDING_FACILITIES_HEADER_INBOX','CT_ET_TEMPLATES.LM_STANDING_FACILITIES_HEADER','INBOX/LM/STANDING_FACILITIES/STANDING_FACILITIES_HEADER');
EXEC CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE('LM_STANDING_FACILITIES_HEADER_ODS','CT_ET_TEMPLATES.LM_STANDING_FACILITIES_HEADER','ODS/LM/STANDING_FACILITIES_HEADER');
EXEC CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE('LM_STANDING_FACILITIES_HEADER_ARCHIVE','CT_ET_TEMPLATES.LM_STANDING_FACILITIES_HEADER','ARCHIVE/LM/STANDING_FACILITIES_HEADER');

-- 3. Export table OU_LM.STANDING_FACILITIES to ARCHIVE

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

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

ALTER TABLE OU_LM.STANDING_FACILITIES_HEADER RENAME TO STANDING_FACILITIES_HEADER_LEGACY;

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

GRANT SELECT ON ods.lm_standing_facilities_header_ods TO OU_LM;
create or replace view OU_LM.STANDING_FACILITIES_HEADER AS SELECT * from ods.LM_STANDING_FACILITIES_HEADER_ods;

