相关疑难解决方法(0)

Oracle:如果表存在

我正在为Oracle数据库编写一些迁移脚本,并且希望Oracle有类似MySQL的IF EXISTS构造.

具体来说,每当我想在MySQL中删除表时,我都会这样做

DROP TABLE IF EXISTS `table_name`;
Run Code Online (Sandbox Code Playgroud)

这样,如果表不存在,DROP则不会产生错误,脚本可以继续.

Oracle是否有类似的机制?我意识到我可以使用以下查询来检查表是否存在

SELECT * FROM dba_tables where table_name = 'table_name';
Run Code Online (Sandbox Code Playgroud)

但是把它与a绑在一起的语法DROP正在逃避我.

sql oracle sql-drop

322
推荐指数
7
解决办法
45万
查看次数

删除表(如果存在)

我有以下PL/SQL:

declare
    i_cnt number;
begin
    select count(1) into i_cnt 
      from dba_tables 
     where table_name = upper('foo') 
       and owner = upper('bar'); 

if i_cnt > 0 then 
    drop table foo; -- <--- error this line
end if;
end;
Run Code Online (Sandbox Code Playgroud)

从中我得到这个错误.

ORA-06550: line 6, column 5:
PLS-00103: Encountered the symbol "DROP" when expecting one of the following:

   ( begin case declare exit for goto if loop mod null pragma
   raise return select update while with <an identifier>
   <a double-quoted delimited-identifier> <a bind variable> <<
   continue …
Run Code Online (Sandbox Code Playgroud)

oracle plsql

4
推荐指数
1
解决办法
6684
查看次数

标签 统计

oracle ×2

plsql ×1

sql ×1

sql-drop ×1