Oracle: If Table Exists

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

    And if you want to make it re-enterable and minimize drop/create cycles, you could cache the DDL using dbms_metadata.get_ddl and re-create everything using a construct like this: declare v_ddl varchar2(4000); begin select dbms_metadata.get_ddl('TABLE','DEPT','SCOTT') into v_ddl from dual; [COMPARE CACHED DDL AND EXECUTE IF NO MATCH] exception when others then if sqlcode = -31603 then [GET AND EXECUTE CACHED DDL] else raise; end if; end; This is just a sample, there should be a loop inside with DDL type, name and owner being variables.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-22 14:16

    I have been looking for the same but I ended up writing a procedure to help me out:

    CREATE OR REPLACE PROCEDURE DelObject(ObjName varchar2,ObjType varchar2)
    IS
     v_counter number := 0;   
    begin    
      if ObjType = 'TABLE' then
        select count(*) into v_counter from user_tables where table_name = upper(ObjName);
        if v_counter > 0 then          
          execute immediate 'drop table ' || ObjName || ' cascade constraints';        
        end if;   
      end if;
      if ObjType = 'PROCEDURE' then
        select count(*) into v_counter from User_Objects where object_type = 'PROCEDURE' and OBJECT_NAME = upper(ObjName);
          if v_counter > 0 then          
            execute immediate 'DROP PROCEDURE ' || ObjName;        
          end if; 
      end if;
      if ObjType = 'FUNCTION' then
        select count(*) into v_counter from User_Objects where object_type = 'FUNCTION' and OBJECT_NAME = upper(ObjName);
          if v_counter > 0 then          
            execute immediate 'DROP FUNCTION ' || ObjName;        
          end if; 
      end if;
      if ObjType = 'TRIGGER' then
        select count(*) into v_counter from User_Triggers where TRIGGER_NAME = upper(ObjName);
          if v_counter > 0 then          
            execute immediate 'DROP TRIGGER ' || ObjName;
          end if; 
      end if;
      if ObjType = 'VIEW' then
        select count(*) into v_counter from User_Views where VIEW_NAME = upper(ObjName);
          if v_counter > 0 then          
            execute immediate 'DROP VIEW ' || ObjName;        
          end if; 
      end if;
      if ObjType = 'SEQUENCE' then
        select count(*) into v_counter from user_sequences where sequence_name = upper(ObjName);
          if v_counter > 0 then          
            execute immediate 'DROP SEQUENCE ' || ObjName;        
          end if; 
      end if;
    end;
    

    Hope this helps

    0 讨论(0)
  • 2020-11-22 14:16

    There is no 'DROP TABLE IF EXISTS' in oracle, you would have to do the select statement.

    try this (i'm not up on oracle syntax, so if my variables are ify, please forgive me):

    declare @count int
    select @count=count(*) from all_tables where table_name='Table_name';
    if @count>0
    BEGIN
        DROP TABLE tableName;
    END
    
    0 讨论(0)
  • 2020-11-22 14:22
    declare
       c int;
    begin
       select count(*) into c from user_tables where table_name = upper('table_name');
       if c = 1 then
          execute immediate 'drop table table_name';
       end if;
    end;
    

    That's for checking whether a table in the current schema exists. For checking whether a given table already exists in a different schema, you'd have to use all_tables instead of user_tables and add the condition all_tables.owner = upper('schema_name')

    0 讨论(0)
  • 2020-11-22 14:22

    A block like this could be useful to you.

    DECLARE
        table_exist INT;
    
    BEGIN
        SELECT Count(*)
        INTO   table_exist
        FROM   dba_tables
        WHERE  owner = 'SCHEMA_NAME' 
        AND table_name = 'EMPLOYEE_TABLE';
    
        IF table_exist = 1 THEN
          EXECUTE IMMEDIATE 'drop table EMPLOYEE_TABLE';
        END IF;
    END;  
    
    0 讨论(0)
提交回复
热议问题