Oracle: If Table Exists

后端 未结 15 1291
无人共我
无人共我 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: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')

提交回复
热议问题