How do I overcome this error and execute the code dynamically?
d mycount s 10p 0 c eval string = 'select count(*) into : mycount from MyTable' * c/exec-sql c+ prepare S1 from string c/end-sql * c/exec-sql c+ execute S1 c/end-sql
The solution is more complicated if you are on a release prior to 6.1 since you need to declare a cursor using dynamic SQL.
DECLARE v1 int; SET string1= 'VALUES(SELECT COUNT(*) from MyTable) INTO ?'; PREPARE s1 from string1; EXECUTE s1 using v1;
DECLARE C1 CURSOR FOR S1 ; SET STRING2 = 'SELECT COUNT(*) FROM MyTable' ; PREPARE S1 FROM STRING2 ; OPEN C1 ; FETCH C1 INTO V1 ;
Dig Deeper on DB2 UDB (universal databases)
Related Q&A from Kent Milligan
Create a host variable of the where in statement on the fly with dynamic SQL. Continue Reading
When working with DB2 files with columns that have both short and long names, there is no option choose which column names are returned via ODBC ... Continue Reading
When developing tables in a parent-child relationship, use a primary key and a foreign key along with a unique ID to make your database easier to ... Continue Reading