In the example used, the SELECT statement that was dynamically run had a constant value in the WHERE clause.
In the example, it was:
WHERE LASTNAME = 'PARKER'
If you want to run the same SELECT statement several times, using different values for LASTNAME, you can use an SQL statement that looks like this:
SELECT WORKDEPT, PHONENO FROM CORPDATA.EMPLOYEE WHERE LASTNAME = ?
When using parameter markers, your application does not need to set the data types and values for the parameters until run time. By specifying a descriptor on the OPEN statement, you can substitute the values for the parameter markers in the SELECT statement.
To code such a program, you need to use the OPEN statement with a descriptor clause. This SQL statement is used to not only open a cursor, but to replace each parameter marker with the value of the corresponding descriptor entry. The descriptor name that you specify with this statement must identify a descriptor that contains a valid definition of the values. This descriptor is not used to return information about data items that are part of a SELECT list. It provides information about values that are used to replace parameter markers in the SELECT statement. It gets this information from the application, which must be designed to place appropriate values into the fields of the descriptor. The descriptor is then ready to be used by SQL for replacing parameter markers with the actual values.
When you use an SQLDA for input to the OPEN statement with the USING DESCRIPTOR clause, not all of its fields need to be filled in. Specifically, SQLDAID, SQLRES, and SQLNAME can be left blank (SQLNAME can be set if a specific CCSID is needed.) Therefore, when you use this method for replacing parameter markers with values, you need to determine:
In addition, if the routine is to handle both SELECT and non-SELECT statements, you might want to determine what category of statement it is.
If your application uses parameter markers, your program has to perform the following steps. This can be done using either an SQLDA or an allocated descriptor.
The statement can then be processed normally.