Oracle: If Table Exists

后端 未结 15 1448
无人共我
无人共我 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:14

    One way is to use DBMS_ASSERT.SQL_OBJECT_NAME :

    This function verifies that the input parameter string is a qualified SQL identifier of an existing SQL object.

    DECLARE
        V_OBJECT_NAME VARCHAR2(30);
    BEGIN
       BEGIN
            V_OBJECT_NAME  := DBMS_ASSERT.SQL_OBJECT_NAME('tab1');
            EXECUTE IMMEDIATE 'DROP TABLE tab1';
    
            EXCEPTION WHEN OTHERS THEN NULL;
       END;
    END;
    /
    

    DBFiddle Demo

提交回复
热议问题