Getting error while trying to alter table in an sql block

Victor picture Victor · Sep 27, 2011 · Viewed 27.9k times · Source

I create a test.sql file and inside I put:

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

Is this not allowed? Because I get error:

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

Answer

Justin Cave picture Justin Cave · Sep 27, 2011

You cannot issue DDL as static SQL in a PL/SQL block. If you want to put those commands in a PL/SQL block, you'd need to use dynamic SQL, i.e.

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

It may be easier, however, to just issue consecutive SQL statements rather than issuing a single PL/SQL block.