user1340582
user1340582

Reputation: 19699

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

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?

Upvotes: 0

Views: 4913

Answers (1)

user1919238
user1919238

Reputation:

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;

Upvotes: 3

Related Questions