【问题标题】:Second 'exception when others' fails第二个“其他人失败时的例外”
【发布时间】: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 不存在,则脚本将失败...我需要捕获此错误...

【问题讨论】:

标签: sql oracle exception-handling


【解决方案1】:

我在这里找到了答案:

Two PLSQL statements with begin and end, run fine separately but not together?

显然我需要

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

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

【讨论】:

    【解决方案2】:

    exception when others then null; 适用于单个开始/结束块。在一个块中包含多个是不是真的有效。您可以通过单独捕获多个块来解决此问题。

    begin
    execute immediate 'drop table X';
    exception when others then null;
    end;
    /
    
    begin
    execute immediate 'drop table Y';
    exception when others then null;
    end;
    /
    

    【讨论】:

    • 谢谢。我试过了......我试过了 ;在第一个END之后,也。不高兴...从第 1 行开始的错误命令 - 开始执行立即“删除表 X”;当其他人为空时异常;结尾;开始执行立即“删除表 Y”;当其他人为空时异常;结尾;错误报告 - ORA-06550: line 5, column 1: PLS-00103: Encountered the symbol "BEGIN" 06550. 00000 - "line %s, column %s:\n%s" *原因:通常是 PL/SQL 编译错误。 *行动:
    猜你喜欢
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    相关资源
    最近更新 更多