尝试在sql块中更改表时获取错误

时间:2011-09-27 21:28:28

标签: oracle oracle11g

我创建了一个test.sql文件,在里面我把:

begin
alter table table1 enable row movement;
alter table table1 shrink space;
end;
/

这是不允许的?因为我收到错误:

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

1 个答案:

答案 0 :(得分:44)

您不能在PL / SQL块中将DDL作为静态SQL发出。如果要将这些命令放在PL / SQL块中,则需要使用动态SQL,即

BEGIN
  EXECUTE IMMEDIATE 'alter table table1 enable row movement';
  EXECUTE IMMEDIATE 'alter table table1 shrink space cascade';
END;
/

然而,发布连续的SQL语句而不是发出单个PL / SQL块可能更容易。