删除表(如果存在)

时间:2012-05-31 17:42:40

标签: oracle plsql

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

如何在程序中删除表?

1 个答案:

答案 0 :(得分:10)

您无法直接从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;