EXECUTE IMMEDIATE Temp table in oracle does not get created ORA-00942

后端 未结 2 1542
执笔经年
执笔经年 2021-01-21 10:07

Based on this answer I was trying to create temp table, however I am getting exception ORA-00942:table or view does not exist I would assume there is something wron

2条回答
  •  粉色の甜心
    2021-01-21 10:29

    You dynamically create the GTT, so your INSERT too should be dynamic..

    Note that, PL/SQL validates every static query before even executing it. Thats why you get ORA-942 Table or view doesn't exist error even at compilation time!

    So, to escape this semantic check, we have to make the call dynamic.

        BEGIN
        EXECUTE IMMEDIATE 'CREATE GLOBAL TEMPORARY TABLE TempQandA(column1 number) ON COMMIT PRESERVE ROWS';
    
    .....
    
        EXECUTE IMMEDIATE ' insert into TempQandA(column1) VALUES (1)';
        END;
    

    And Finally, you should not be creating the GTT on run time.. To avoid such issues. The GTT is anyway going to local to every session.

    EDIT: As Lalit says, GTT's DDL doesn't accept CREATE OR REPLACE

提交回复
热议问题