How to get DB2 9.7 cursor value in for loop where returned value is a string

戏子无情 提交于 2019-12-25 05:28:46

问题


I have a loop e.g.

DECLARE SQL VARCHAR(1024);

FOR V AS CUR1 CURSOR FOR (SELECT 'Select * from My_Table' from ... )
DO
    SET SQL = CUR1;
    PREPARE S1 FROM SQL;
    EXECUTE S1;
END FOR;

This does not seem to work. I have successfully done similar to this when the cursor in the for loop points to some row returned and I can access the value e.g. like this:

SET SQL = CUR1.TABNAME

But now that the returned value is a string, I have no idea how to get it from the cursor. Any ideas?


回答1:


You simply need to specify a column name in your query:

DECLARE SQL VARCHAR(1024);

FOR V AS CUR1 CURSOR FOR (SELECT 'drop table My_Table' as malicious_code from...)
DO
    SET SQL = CUR1.malicious_code;
    PREPARE S1 FROM SQL;
    EXECUTE S1;
END FOR;


来源:https://stackoverflow.com/questions/15198577/how-to-get-db2-9-7-cursor-value-in-for-loop-where-returned-value-is-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!