加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长百科 > 正文

oracle – 如果存在则删除表

发布时间:2021-04-04 09:18:57 所属栏目:站长百科 来源:网络整理
导读:我有以下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 lineend if;end; 从中我得到这个错误. ORA-06

我有以下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;

从中我得到这个错误.

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 close current delete fetch lock insert open rollback
   savepoint set sql execute commit forall merge pipe purge

如何在程序中删除表格?

解决方法

您不能直接从PL / SQL块执行DDL语句 – 您必须使用EXECUTE IMMEDIATE:

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 
    execute immediate 'drop table foo'; 
  end if;
end;

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读