今天早学习DB2游标的时候,发现老是执行不通过,找了半天也找不到原因,后来发现竟然和游标声明的位置有关。
游标声明需要放在下面2个的中间
DECLARE NOT_FOUND CONDITION FOR SQLSTATE '02000';
DECLARE MYCURSOR CURSOR FOR SELECT ID FROM T;
DECLARE CONTINUE HANDLER FOR NOT_FOUND SET V_END = 1;
如下是正确的。
BEGIN
DECLARE V_STR VARCHAR(10) DEFAULT '';
DECLARE V_TOTAL VARCHAR(10) DEFAULT '';
DECLARE V_END INT DEFAULT 0;
DECLARE NOT_FOUND CONDITION FOR SQLSTATE '02000';
DECLARE MYCURSOR CURSOR FOR SELECT ID FROM T;
DECLARE CONTINUE HANDLER FOR NOT_FOUND SET V_END = 1;
OPEN MYCURSOR;
LOOP1: LOOP
FETCH MYCURSOR INTO V_STR;
IF V_END=0 THEN
IF V_STR='3' THEN
ITERATE LOOP1;
END IF;
SET V_TOTAL=V_TOTAL||V_STR||';';
ELSE
LEAVE LOOP1;
END IF;
END LOOP;
INSERT INTO LOGTAB VALUES(V_TOTAL);
CLOSE MYCURSOR;
END@
db2 => BEGIN
db2 (cont.) => DECLARE V_STR VARCHAR(10) DEFAULT '';
db2 (cont.) => DECLARE V_TOTAL VARCHAR(10) DEFAULT '';
db2 (cont.) => DECLARE V_END INT DEFAULT 0;
db2 (cont.) => DECLARE NOT_FOUND CONDITION FOR SQLSTATE '02000';
db2 (cont.) => DECLARE MYCURSOR CURSOR FOR SELECT ID FROM T;
db2 (cont.) => DECLARE CONTINUE HANDLER FOR NOT_FOUND SET V_END = 1;
db2 (cont.) => OPEN MYCURSOR;
db2 (cont.) => LOOP1: LOOP
db2 (cont.) => FETCH MYCURSOR INTO V_STR;
db2 (cont.) => IF V_END=0 THEN
db2 (cont.) => IF V_STR='3' THEN
db2 (cont.) => ITERATE LOOP1;
db2 (cont.) => END IF;
db2 (cont.) => SET V_TOTAL=V_TOTAL||V_STR||';';
db2 (cont.) => ELSE
db2 (cont.) => LEAVE LOOP1;
db2 (cont.) => END IF;
db2 (cont.) => END LOOP;
db2 (cont.) => INSERT INTO LOGTAB VALUES(V_TOTAL);
db2 (cont.) => CLOSE MYCURSOR;
db2 (cont.) => END@
DB20000I SQL 命令成功完成。
db2 => BEGIN
db2 (cont.) => DECLARE V_STR VARCHAR(10) DEFAULT '';
db2 (cont.) => DECLARE V_TOTAL VARCHAR(10) DEFAULT '';
db2 (cont.) => DECLARE V_END INT DEFAULT 0;
db2 (cont.) => DECLARE MYCURSOR CURSOR FOR SELECT ID FROM T;
db2 (cont.) => DECLARE NOT_FOUND CONDITION FOR SQLSTATE '02000';
db2 (cont.) => DECLARE CONTINUE HANDLER FOR NOT_FOUND SET V_END = 1;
db2 (cont.) => OPEN MYCURSOR;
db2 (cont.) => LOOP1: LOOP
db2 (cont.) => FETCH MYCURSOR INTO V_STR;
db2 (cont.) => IF V_END=0 THEN
db2 (cont.) => IF V_STR='3' THEN
db2 (cont.) => ITERATE LOOP1;
db2 (cont.) => END IF;
db2 (cont.) => SET V_TOTAL=V_TOTAL||V_STR||';';
db2 (cont.) => ELSE
db2 (cont.) => LEAVE LOOP1;
db2 (cont.) => END IF;
db2 (cont.) => END LOOP;
db2 (cont.) => INSERT INTO LOGTAB VALUES(V_TOTAL);
db2 (cont.) => CLOSE MYCURSOR;
db2 (cont.) => END@
DB21034E 该命令被当作 SQL 语句来处理,因为它是无效的“命令行处理器”命令。在
SQL 处理期间,它返回:
SQL0104N 在 "" 后面找到异常标记 "declaration>"。预期标记可能包括:""。 LINE NUMBER=6.
SQLSTATE=42601
db2 => BEGIN
db2 (cont.) => DECLARE V_STR VARCHAR(10) DEFAULT '';
db2 (cont.) => DECLARE V_TOTAL VARCHAR(10) DEFAULT '';
db2 (cont.) => DECLARE V_END INT DEFAULT 0;
db2 (cont.) => DECLARE NOT_FOUND CONDITION FOR SQLSTATE '02000';
db2 (cont.) => DECLARE CONTINUE HANDLER FOR NOT_FOUND SET V_END = 1;
db2 (cont.) => DECLARE MYCURSOR CURSOR FOR SELECT ID FROM T;
db2 (cont.) => OPEN MYCURSOR;
db2 (cont.) => LOOP1: LOOP
db2 (cont.) => FETCH MYCURSOR INTO V_STR;
db2 (cont.) => IF V_END=0 THEN
db2 (cont.) => IF V_STR='3' THEN
db2 (cont.) => ITERATE LOOP1;
db2 (cont.) => END IF;
db2 (cont.) => SET V_TOTAL=V_TOTAL||V_STR||';';
db2 (cont.) => ELSE
db2 (cont.) => LEAVE LOOP1;
db2 (cont.) => END IF;
db2 (cont.) => END LOOP;
db2 (cont.) => INSERT INTO LOGTAB VALUES(V_TOTAL);
db2 (cont.) => CLOSE MYCURSOR;
db2 (cont.) => END@
DB21034E 该命令被当作 SQL 语句来处理,因为它是无效的“命令行处理器”命令。在
SQL 处理期间,它返回:
SQL0104N 在 "" 后面找到异常标记 "declaration>"。预期标记可能包括:""。 LINE NUMBER=7.
SQLSTATE=42601
db2 =>
这个规则也够蛋疼的,感觉还是ORACLE的游标灵活和方便。
阅读(6864) | 评论(0) | 转发(0) |