【发布时间】:2018-10-11 15:55:48
【问题描述】:
我正在尝试使用 EXCEPTION WHEN OTHERS 来捕获我尝试 DROP 的不存在的表,如下所示:
begin
execute immediate 'drop table X';
exception when others then null;
end;
如果表 x 存在并且我运行它,则该表被删除并且一切正常。如果我再次运行它,没有要删除的表,但是 EXCEPTION 会导致脚本顺利进行。到现在为止还挺好。
如果我多次尝试这样做,就会出现问题。
如果我运行此脚本来删除表 X 和 Y:
begin
execute immediate 'drop table X';
exception when others then null;
execute immediate 'drop table Y';
exception when others then null;
end;
我收到以下错误信息:
从第 1 行开始的错误命令 -
begin
execute immediate 'drop table X';
exception when others then null;
execute immediate 'drop table Y';
exception when others then null;
end;
错误报告 - ORA-06550:第 6 行,第 1 列: PLS-00103:在预期以下情况之一时遇到符号“例外”:
( begin case declare end exit for goto if loop mod null 当使用时,pragma raise 返回选择更新
end not pragma final 可实例化顺序覆盖静态 成员构造函数映射 06550. 00000 - “第 %s 行,第 %s 列:\n%s” *原因:通常是 PL/SQL 编译错误。 *行动:
我在这里缺少什么?如果我删除了第二个 EXCEPTION WHEN 语句,如果表 Y 不存在,则脚本将失败...我需要捕获此错误...
【问题讨论】:
-
不要使用“when others then null”,因为可能会发生其他异常。相反,捕获您期望的异常:stackoverflow.com/a/1801453/103295
标签: sql oracle exception-handling