我正在为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正在逃避我.
我有以下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)