Oracle: If Table Exists

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

    I prefer to specify the table and the schema owner.

    Watch out for case sensitivity as well. (see "upper" clause below).

    I threw a couple of different objects in to show that is can be used in places besides TABLEs.

    .............

    declare
       v_counter int;
    begin
     select count(*) into v_counter from dba_users where upper(username)=upper('UserSchema01');
       if v_counter > 0 then
          execute immediate 'DROP USER UserSchema01 CASCADE';
       end if; 
    end;
    /
    
    
    
    CREATE USER UserSchema01 IDENTIFIED BY pa$$word
      DEFAULT TABLESPACE users
      TEMPORARY TABLESPACE temp
      QUOTA UNLIMITED ON users;
    
    grant create session to UserSchema01;  
    

    And a TABLE example:

    declare
       v_counter int;
    begin
     select count(*) into v_counter from all_tables where upper(TABLE_NAME)=upper('ORDERS') and upper(OWNER)=upper('UserSchema01');
       if v_counter > 0 then
          execute immediate 'DROP TABLE UserSchema01.ORDERS';
       end if; 
    end;
    /   
    

提交回复
热议问题