How to get DB2 9.7 cursor value in for loop where

2019-08-19 02:11发布

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条回答
beautiful°
2楼-- · 2019-08-19 02:21

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;
查看更多
登录 后发表回答