create or replace PROCEDURE            "UPDATE_ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_PROC" 
    AUTHID CURRENT_USER
IS
BEGIN
    dbms_output.put_line('Procedure UPDATE_ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_PROC started.');
    BEGIN
        EXECUTE IMMEDIATE 'DROP TABLE ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_TMP';
        dbms_output.put_line('Dropped table ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_TMP.');
    EXCEPTION
        WHEN OTHERS THEN
            dbms_output.put_line('Table ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_TMP doesn''t exists.');
    END;
    BEGIN
        EXECUTE IMMEDIATE 'DROP TABLE ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_DEL';
        dbms_output.put_line('Dropped table ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_DEL.');
    EXCEPTION
        WHEN OTHERS THEN
            dbms_output.put_line('Table ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_DEL doesn''t exists.');
    END;
    COMMIT;
    EXECUTE IMMEDIATE 'CREATE TABLE ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_TMP as (

SELECT * FROM OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_VW

    )';
    dbms_output.put_line('Created table ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_TMP using pre-defined SQL.');
    COMMIT;
    BEGIN
        EXECUTE IMMEDIATE 'RENAME ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB to ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_DEL';
        dbms_output.put_line('Renamed table ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB to ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_DEL.');
    EXCEPTION
        WHEN OTHERS THEN
            dbms_output.put_line('Table ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB doesn''t exists.');
    END;
    BEGIN
        EXECUTE IMMEDIATE 'RENAME ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_TMP to ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB';
        dbms_output.put_line('Renamed table ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_TMP to ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB.');
    EXCEPTION
        WHEN OTHERS THEN
            dbms_output.put_line('Table ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_TMP doesn''t exists.');
    END;
    BEGIN
        EXECUTE IMMEDIATE 'DROP TABLE ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_DEL';
        dbms_output.put_line('Dropped table ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_DEL.');
    EXCEPTION
        WHEN OTHERS THEN
            dbms_output.put_line('Table ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_DEL doesn''t exists.');
    END;
    BEGIN
        EXECUTE IMMEDIATE 'GRANT SELECT ON ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB TO USER_MIGVAL';
        dbms_output.put_line('Granted user USER_MIGVAL select permission on table ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB.');
    END;
    /*BEGIN
        EXECUTE IMMEDIATE 'CREATE UNIQUE INDEX INDEX_ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB ON ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB (STYPE, PERIOD)';
        dbms_output.put_line('Created index on ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB.');
    END;
    BEGIN
        EXECUTE IMMEDIATE 'ALTER TABLE ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB MODIFY (STYPE NOT NULL ENABLE)';
    EXCEPTION
        WHEN OTHERS THEN
            dbms_output.put_line('Column is already NOT NULL.');
    END;
    BEGIN
        EXECUTE IMMEDIATE 'ALTER TABLE ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB MODIFY (PERIOD NOT NULL ENABLE)';
    EXCEPTION
        WHEN OTHERS THEN
            dbms_output.put_line('Column is already NOT NULL.');
    END;
    BEGIN
        EXECUTE IMMEDIATE 'ALTER TABLE ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB ADD CONSTRAINT INDEX_ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB PRIMARY KEY (STYPE, PERIOD)';
        dbms_output.put_line('Created constrain on ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB.');
    END;*/
    COMMIT;
    dbms_output.put_line('Procedure UPDATE_ON_DEMAND_OSEARCH_MIGRATETELEMETRY_MIGVAL_MIGRATES_TB_PROC finished successfully.');
END;