sql - 第二个“其他人失败时的例外”
问题描述
我正在尝试使用 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 不存在,则脚本将失败...我需要捕获此错误...
解决方案
我在这里找到了答案:
两个带有开始和结束的PLSQL语句,单独运行但不能一起运行?
显然我需要
begin
begin
some stuff
end;
begin
some other stuff
end;
end;
或者在两个内部块 END 之后放置一个 / ;的...
推荐阅读
- shell - 将 find 与 printf 和 or 一起使用
- python - 将 x、y、值时间序列插入另一组点的最简单方法是什么?
- android - 粘贴到多个edittext
- tcp - TCP 端口和 OSI 模型
- git - 合并 develop 到 master 使得 commit 在顶部移动
- mysql - 使用 case 语句从 SELECT 更新记录。(mysql)
- java - 将 Java 客户端 (JMS) 连接到 IBM MQ 时出现问题
- python - 如何在列表中查找重复值并将它们合并
- java - Spring Batch Job 完成时记录
- php - 内置服务器 PHP 的 Google Cloud Shell 问题 - 无法连接到端口 8080 上的 Cloud Shell