I am trying to use EXCEPTION WHEN OTHERS to catch non-existent tables that I try to DROP, as follows:
begin
execute immediate 'drop table X';
exception when others then null;
end;
If table x exists and I run this, the table is dropped and all is well. If I run it again, there is no table to be dropped, but the EXCEPTION thing results in the script proceeding happily. So far, so good.
The problem appears if I try to do this more than once.
If I run this script to drop tables X and Y:
begin
execute immediate 'drop table X';
exception when others then null;
execute immediate 'drop table Y';
exception when others then null;
end;
I get the following error information:
Error starting at line : 1 in command -
begin
execute immediate 'drop table X';
exception when others then null;
execute immediate 'drop table Y';
exception when others then null;
end;
Error report - ORA-06550: line 6, column 1: PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following:
( begin case declare end exit for goto if loop mod null pragma raise return select update when while with << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge ORA-06550: line 7, column 4: PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
end not pragma final instantiable order overriding static member constructor map 06550. 00000 - "line %s, column %s:\n%s" *Cause: Usually a PL/SQL compilation error. *Action:
What am I missing here? If I removed the second EXCEPTION WHEN statement, the script fails if table Y doesn't exist... I need to catch this error...