首页 > 解决方案 > 未触发 SQL 继续处理程序

问题描述

IBM i V7R1M0。每当发生错误时,我需要继续处理一条语句,据我所知,例如:

https://dba.stackexchange.com/questions/88862/how-to-ignore-sql-errors-in-stored-procedure-not-handle

DECLARE CONTINUE HANDLER 似乎是答案,所以

我有一个非常简单的过程,如下所示:

exec SQL create or replace procedure test_prod1   
         (in test2 decimal(1,0))                  
         language sql modifies sql data           
         begin                                    
         declare continue handler for sqlexception
            begin end;                            

         update DUPEPF set INT2 = test2;          
         end;                                     

据我所知,这意味着无论何时发生错误(例如违反唯一键),SQL 语句都会继续执行,但事实并非如此。每当出现键违规并且未处理下一行时,该语句就会停止。我很困惑为什么会这样

标签: sqldb2-400

解决方案


您的继续处理程序正在工作...

UPDATE您的过程忽略语句引发的错误并继续。除了没有别的事可做。

仅仅因为您的 proc 忽略了错误,并不意味着数据库可以忽略其更新语句处理中的错误。

EDIT
处理程序改变了您的存储过程或 UDF 处理错误的方式......将它们视为“捕获”数据库抛出的错误的一种方式。他们不会阻止数据库首先抛出这些错误

说得通?

为了做你想做的事,你需要使用自己的光标,像这样......

     create or replace procedure test_prod1   
     (in test2 decimal(1,0))                  
     language sql modifies sql data           
     begin           
     declare myInt integer;
     DECLARE DUPLICATE_KEY CONDITION FOR SQLSTATE '23505';
     DECLARE END_OF_TABLE CONDITION FOR SQLSTATE '02000';
     declare test_cursor cursor for
        select int2 from DUPEPP for update;
     declare exit handler for END_OF_TABLE
        close test_cursor;                         
     declare continue handler for DUPLICATE_KEY
        begin end;                            


     open test_cursor;
     fetch_loop:
     LOOP
       fetch next from test_cursor into myInt;
       update dupepf set int2 = test2
         where current of test_cursor;
     END LOOP fetch_loop;
     end;

推荐阅读