Dynamically executing a statement with parameter markers by using the SQLDA

Your program can get data type information about parameter markers by asking Db2 to set the fields in the SQLDA.

Before you begin

Before you dynamically execute a statement with parameter markers, allocate an SQLDA with enough instances of SQLVAR to represent all parameter markers in the SQL statement.

Procedure

To dynamically execute a statement with parameter markers by using the SQLDA:

  1. Include in your program a DESCRIBE INPUT statement that specifies the prepared SQL statement and the name of an appropriate SQLDA.

    Db2 puts the requested parameter marker information in the SQLDA.

  2. Code the application in the same way as any other application in which you execute a prepared statement by using an SQLDA. First, obtain the addresses of the input host variables and their indicator variables and insert those addresses into the SQLDATA and SQLIND fields. Then, execute the prepared SQL statement.

Example

Suppose that you want to execute the following statement dynamically:

DELETE FROM DSN8C10.EMP WHERE EMPNO = ?

You can use the following code to set up an SQLDA, obtain parameter information by using the DESCRIBE INPUT statement, and execute the statement:

SQLDAPTR=ADDR(INSQLDA);          /* Get pointer to SQLDA         */
SQLDAID='SQLDA';                 /* Fill in SQLDA eye-catcher    */
SQLDABC=LENGTH(INSQLDA);         /* Fill in SQLDA length         */
SQLN=1;                          /* Fill in number of SQLVARs    */
SQLD=0;                          /* Initialize # of SQLVARs used */
DO IX=1 TO SQLN;                 /* Initialize the SQLVAR        */
  SQLTYPE(IX)=0;
  SQLLEN(IX)=0;
  SQLNAME(IX)='';
END;
SQLSTMT='DELETE FROM DSN8C10.EMP WHERE EMPNO = ?';
EXEC SQL PREPARE SQLOBJ FROM SQLSTMT;
EXEC SQL DESCRIBE INPUT SQLOBJ INTO :INSQLDA;
SQLDATA(1)=ADDR(HVEMP);          /* Get input data address       */
SQLIND(1)=ADDR(HVEMPIND);        /* Get indicator address        */
EXEC SQL EXECUTE SQLOBJ USING DESCRIPTOR :INSQLDA;