0

DB2 V9 Z/Os

CREATE PROCEDURE SERDB.I21MMSNOUPD () 
RESULT SETS 1
LANGUAGE SQL
FENCED
COLLID SER
WLM ENVIRONMENT DDSNSPENV
RUN OPTIONS 'NOTEST(NONE,*,*,*)'

P1: BEGIN

--Declare variables
DECLARE CONSUMER        INTEGER;
DECLARE NEW_MMS_NO      INTEGER;
DECLARE END_TABLE       INT DEFAULT 0;


DECLARE C1 CURSOR FOR 
SELECT I20_CONSUMER_ID,
       NEW_MMS_NO
  FROM SERDB.I20_TEMP
-- WHERE I20_CONSUMER_ID = 164921;
ORDER BY I20_CONSUMER_ID;

DECLARE CONTINUE HANDLER FOR NOT FOUND
 SET END_TABLE = 1;  

OPEN C1;
FETCH C1 INTO CONSUMER,
          NEW_MMS_NO;

WHILE END_TABLE = 0 DO              

UPDATE SERDB.I20_CONSUMER_T
   SET I20_MMS_NO = NEW_MMS_NO
 WHERE I20_CONSUMER_ID = CONSUMER;

END WHILE;

CLOSE C1;              
END P1

The above stored procedure builds with a cond code 0, but fails to execute even when a specific consumer_id. Does anyone see something wrong?

Individual sql statements run exactly as they're supposed to.

I've followed the examples for Cursors in SQL Procedures from IBM.

Thank you

4

2 回答 2

1

我 100% 同意@X-Zero,当您可以执行简单的基于集合的操作(可能具有更好的性能)时,这似乎是定义游标和其他东西的大量工作。以下是两个示例,说明如何通过单个操作完成此操作:

正常UPDATE

UPDATE SESSION.I20_CONSUMER_T A
SET I20_MMS_NO = (
    SELECT NEW_MMS_NO
    FROM SESSION.I20_TEMP B
    WHERE A.I20_CONSUMER_ID = B.CONSUMER
)
WHERE EXISTS (
    SELECT 1
    FROM SESSION.I20_TEMP C
    WHERE A.I20_CONSUMER_ID = C.CONSUMER
)

MERGE热点:

MERGE INTO SESSION.I20_CONSUMER_T AS T
USING SESSION.I20_TEMP AS M
   ON T.I20_CONSUMER_ID = M.CONSUMER
WHEN MATCHED THEN
    UPDATE SET T.I20_MMS_NO = M.NEW_MMS_NO
ELSE IGNORE

这些在 DB2 for Linux/Unix/Windows v9.7 上进行了测试,但应该可以在比 9.1 更新的任何 DB2 版本上工作(DB2 for iSeries 是一个通配符,我不记得那个平台支持或不支持什么:)

于 2012-08-29T16:10:00.290 回答
-1

FETCH 命令必须在 WHILE 内,以便每次调用它时,它都会获取一行。

于 2015-03-30T16:47:52.317 回答