日期:2014-05-17  浏览次数:20910 次

很简单一个存储过程,目的是想在第一条语句触发异常后,继续执行下面语句,不知道如何实现这样的功能。
很简单一个存储过程,目的是想在第一条语句触发异常后,继续执行下面语句,不知道如何实现这样的功能。
create or replace procedure pro_update_idcard
as
--这只是一个测试用例,实际程序有很多语句,但就是想有异常也能继续往下执行后面没错的语句。
begin
  for i in(select * from yjf_dongcheng)
  loop 
--语句1:此语句触发主见约束异常,表中有为2的.
     update yjf_dongcheng t set t.anv04yjfid='1' where t.anv04yjfid='2'; 
--语句2:想让下面这条语句继续执行
     update yjf_dongcheng t set t.anv04yjfid='1' where t.anv04yjfid='2a';
     commit;
--最终第二条语句提交。
  end loop;

end;
------解决方案--------------------

create or replace procedure pro_update_idcard
as
--这只是一个测试用例,实际程序有很多语句,但就是想有异常也能继续往下执行后面没错的语句。
begin
  for i in(select * from yjf_dongcheng)
  loop  
    --语句1:此语句触发主见约束异常,表中有为2的.
    begin
     update yjf_dongcheng t set t.anv04yjfid='1' where t.anv04yjfid='2';  
    exception
      when others then
      --语句2:想让下面这条语句继续执行
       update yjf_dongcheng t set t.anv04yjfid='1' where t.anv04yjfid='2a';
    end;
  
    commit;
    --最终第二条语句提交。
  end loop;

end;


------解决方案--------------------
BEGIN
    ...............
    --使用begin..exception..end块
    BEGIN
      UPDATE yjf_dongcheng t
         SET t.anv04yjfid = '1'
       WHERE t.anv04yjfid = '2';
    EXCEPTION
      WHEN OTHERS THEN
        NULL;--不处理异常
    END;
    --继续执行其它语句
    .................
END;