关于plsql执行时异常想要继续执行的问题

在项目中 我们会经常遇到用plsql写存储过程 遍历游标往表里面插入数据的问题
但是如果安装正常情况写的话 如果中途有异常抛出 那么就会不继续执行了

但是 还有种情况就是我们需要他继续插入 不能插入抛出异常的记录下来 不用影响后续的插入
解决的办法是嵌套begin end; 把会有可能抛出异常的代码放到这个嵌套的begin end里面去

declare
myexception exception;
begin
  for i in 1..10
   loop
    begin
      raise myexception;
      --insert....这里为了简单 直接抛出异常
      exception
      when others
        then
          Dbms_Output.put_line('抛出异常'||'>>'||i);
          --这里可以定义一个表 把错误的写进去 
    end;
     Dbms_Output.put_line('继续执行'||'>>'||i); 
  end loop;
end;


ok...

猜你喜欢

转载自hanyingjun318.iteye.com/blog/2092958