Preparing Statements That Receive Parameters
In some statements, parameters are unknown when the statement is prepared because a different value can be inserted each time the statement is executed. In these statements, you can use a question-mark ( ? ) placeholder where a parameter must be supplied when the statement is executed.
The PREPARE statements in the following examples
show some uses of question-mark ( ? ) placeholders:
EXEC SQL prepare s3 from 'select * from customer where state matches ?'; EXEC SQL prepare in1 from 'insert into manufact values (?,?,?)'; sprintf(up_query, "%s %s", "update customer set zipcode = ?" "where current of zip_cursor"); EXEC SQL prepare update2 from :up_query; EXEC SQL prepare exfunc from 'execute function func1 (?, ?)';
You can use a placeholder to defer evaluation of a value until runtime only for an expression, but not for an SQL identifier, except as noted in Preparing Statements with SQL Identifiers.
The following example of the code
fragment prepares a statement from a variable that is named demoquery.
The text in the variable includes one question-mark ( ? ) placeholder.
The prepared statement is associated with a cursor and, when the cursor
is opened, the USING clause of the OPEN statement supplies a value
for the placeholder:
EXEC SQL BEGIN DECLARE SECTION; char queryvalue [6]; char demoquery [80]; EXEC SQL END DECLARE SECTION; EXEC SQL connect to 'stores_demo'; sprintf(demoquery, "%s %s", "select fname, lname from customer ", "where lname > ? "); EXEC SQL prepare quid from :demoquery; EXEC SQL declare democursor cursor for quid; stcopy("C", queryvalue); EXEC SQL open democursor using :queryvalue;
The USING clause is available in both OPEN statements that are associated with a cursor and EXECUTE statements (all other prepared statements).
You can use a question-mark ( ? ) placeholder to represent the name of the or SPL collection variable.