oracle获取循环中的异常信息,zzw原创_oracle循环中的异常捕捉_捕捉异常后并继续循环...

例子如下

set serveroutput on;

declare

V_SQL VARCHAR2(255);

errorCode number; --异常编码

errorMsg varchar2(1000); --异常信息

out_return varchar2(1000);

flag varchar2(10);

CURSOR TP IS

SELECT TABLE_NAME,CONSTRAINT_NAME FROM USER_CONSTRAINTS  WHERE CONSTRAINT_TYPE = 'P';

begin

FOR E IN TP LOOP

BEGIN

V_SQL := 'ALTER TABLE ' || E.TABLE_NAME || ' ' || 'DISABLE' ||'  '|| 'CONSTRAINT ' || E.CONSTRAINT_NAME;

DBMS_OUTPUT.PUT_LINE(V_SQL);

EXECUTE IMMEDIATE (V_SQL);

EXCEPTION

when others then

errorCode := SQLCODE;

errorMsg := SUBSTR(SQLERRM, 1, 200);

flag := 'false';

out_return := 'flag=' || flag || ',errorCode=' || errorCode || ',errorMsg=' || errorMsg;

dbms_output.put_line(out_return);

null;

END;

END LOOP;

end;

注意问题

1、捕捉异常后继续下一次循环需用

EXCEPTION

when others then  null;

这样的结构。

2、在for ...LOOP   ENDLOOP 循环中捕捉异常,必须用begin   end 包起来,否则会报错。在PLSQL中报

PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following

Error: PLS-00103: Encountered the symbol "END" when expecting one of the following

正确

FOR E IN TP LOOP

begin

......................

EXCEPTION

when others then  null;

end;

endloop;

错误用法:

FOR E IN TP LOOP......................

EXCEPTION

when others then  null;

endloop;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值