我不确定我的代码有什么问题,我正在尝试创建一个while循环,它将使用SQL中声明的游标获取字段(代码)中的下一个字符。目标是通过循环获得下一个匹配的字符。然后我想在循环结束时返回结果。如果没有完全匹配,目标是与代码进行部分匹配。我之前从未使用过游标,因此我尝试尽可能多地学习使用fetch和游标。
EXEC SQL
SELECT field FROM file
WHERE field = :code
UNION
DECLARE UserInput CURSOR FOR
SELECT field FROM file
WHERE field LIKE '%' || :code || '%'
ORDER BY field ASC
OPEN UserInput
FETCH NEXT FROM UserInput
BEGIN
DO WHILE <> %EOF
FETCH NEXT FROM UserInput
END
CLOSE UserInput
DEALLOCATE UserInput;
答案 0 :(得分:8)
%EOF()
适用于RPG记录级别访问,您需要进行检查
SQLCODE或SQLSTATE FETCH
OPEN
都是SQL语句,需要位于EXEC SQL
DEALLOCATE
不需要FETCH
从光标到RPG变量的行看看这段代码:
EXEC SQL
DECLARE UserInput CURSOR FOR
SELECT field FROM file
WHERE field = :code
UNION
SELECT field FROM file
WHERE field LIKE '%' || :code || '%'
ORDER BY field ASC;
EXEC SQL
OPEN UserInput;
--really should check SQLSTATE here too!
EXEC SQL
FETCH NEXT FROM UserInput INTO :MyRpgVar;
Dow SQLSTATE = '00000';
--note 00000 = no errors or warning
-- 02000 = no data
<do somthing?>
EXEC SQL
FETCH NEXT FROM UserInput INTO :MyRpgVar;
ENDDO;
EXEC SQL
CLOSE UserInput;
我建议您阅读嵌入式SQL编程参考的RPGLE部分。