首页 > 解决方案 > 第二个“其他人失败时的例外”

问题描述

我正在尝试使用 EXCEPTION WHEN OTHERS 来捕获我尝试删除的不存在的表,如下所示:

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:在预期以下情况之一时遇到符号“例外”:

(开始 case 声明 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:第 7 行,第 4 列: PLS-00103:在预期以下情况之一时遇到符号“文件结尾”:

end not pragma final 可实例化顺序覆盖静态成员构造函数映射 06550。00000 -“行 %s,列 %s:\n%s” *原因:通常是 PL/SQL 编译错误。*行动:

我在这里想念什么?如果我删除了第二个 EXCEPTION WHEN 语句,如果表 Y 不存在,则脚本将失败...我需要捕获此错误...

标签: sqloracleexception-handling

解决方案


我在这里找到了答案:

两个带有开始和结束的PLSQL语句,单独运行但不能一起运行?

显然我需要

begin
    begin
        some stuff
    end;
    begin
        some other stuff
    end;
end;

或者在两个内部块 END 之后放置一个 / ;的...


推荐阅读