Oracle: If Table Exists

后端 未结 15 1558
无人共我
无人共我 2020-11-22 13:32

I\'m writing some migration scripts for an Oracle database, and was hoping Oracle had something similar to MySQL\'s IF EXISTS construct.

Specifically, w

15条回答
  •  孤独总比滥情好
    2020-11-22 14:02

    just wanted to post a full code that will create a table and drop it if it already exists using Jeffrey's code (kudos to him, not me!).

    BEGIN
        BEGIN
             EXECUTE IMMEDIATE 'DROP TABLE tablename';
        EXCEPTION
             WHEN OTHERS THEN
                    IF SQLCODE != -942 THEN
                         RAISE;
                    END IF;
        END;
    
        EXECUTE IMMEDIATE 'CREATE TABLE tablename AS SELECT * FROM sourcetable WHERE 1=0';
    
    END;
    

提交回复
热议问题