Toad World® Forums

How fill the '?' parameter in the sql query

Hello all,

I’m brand new in DB2. I’m trying to create some procedures but I’m failing. The problem is this: I got a table, where there’s a specific SQL to be run with a parameter ‘?’ which has to be filled:

table name: TASKS

ID
SQL
1
insert into students (firstName, lastName, checkId) select firstName, lastName, ? from kids where age = 18
2
insert into students (firstName, lastName, checkId) select firstName, lastName, ? from kids where sex = ‘female’
3
insert into students (firstName, lastName, checkId) select firstName, lastName, ? from kids where grade = ‘b+’

and so on.

The exercise is:

  1. select the ID and SQL from above table;

  2. Run the selected SQL but instead of the parameter ‘?’ in the SQL set the value of ID.

This is what I got:

begin

declare idNumber INTEGER;

for std as

select ID idValue, SQL sqltext from TASKS

do

set idNumber = idvalue;

execute immediate sqltext using idNumber;

end;

But i’m still failing. I tried: “execute immediate sqltext :idNumber” and so on, but it doesn’t work. I’ve read about passing variables into sql and parameters ‘?’ but no solution works. I’d be happy anyone explain what’s wrong.

Best regards,

Mike

Hi Mike,

You can’t use parameter markers with EXECUTE IMMEDIATE. You might use a PREPARE and EXECUTE.

For answers to all your questions, the first place to look is IBM documentation.
Try this link - http://www-01.ibm.com/support/knowledgecenter/SSEPGG_10.5.0/com.ibm.db2.luw.sql.ref.doc/doc/c0004100.html?cp=SSEPGG_10.5.0%2F2-12&lang=en

Regards,
Adam