Handling a parameterized SELECT statement

You can handle a parameterized SELECT statement with an sqlda structure.

About this task

If a prepared SELECT statement has a WHERE clause with input parameters of unknown number and data type, your program must use an sqlda structure to define the input parameters.

For an introduction on how to determine input parameters, see Determine unknown input parameters.

To use an sqlda structure to define input parameters for a WHERE clause:

Procedure

  1. Declare a variable to hold the address of an sqlda structure.
    For more information, see Define an sqlda structure.
  2. Determine the number and data types of the input parameters of the SELECT statement.
    For more information, see Determine unknown input parameters.
  3. Allocate an sqlda structure with a system memory-allocation function such as malloc().
  4. Indicate the number of input parameters in the WHERE clause with C-language statements that set the sqld field of the sqlda structure.
  5. Store the definitions and values of each input parameter with C-language statements that set the sqltype, sqllen, and sqldata fields in the appropriate sqlvar_struct of the sqlda structure:
    • The sqltype field uses the data type constants, which the sqltypes.h header file defines, to represent the data type of the input parameter. For more information, see Determine the data type of a column.
    • For a CHAR or VARCHAR value, sqllen is the size, in bytes, of the character array. For a DATETIME or INTERVAL value, this field stores the encoded qualifiers.
    • The sqldata field of each sqlvar_struct structure contains the address of the memory allocated for the input parameter value. You might need to use the sqltype and sqllen fields for each input parameter to determine the amount of memory that is required to allocate. For more information, see Allocate memory for column data.

    If you use an indicator variable, also set the sqlind field and perhaps the sqlidata, sqlilen, and sqlitype fields (for non-X/Open applications only).

    Use an index into the sqlda.sqlvar array to identify the sqlvar_struct structure. For more information, see Assign and obtain values from an sqlda structure.

  6. Pass the defined input parameters from the sqlda structure to the database server with the USING DESCRIPTOR clause.

    The statement that provides the input parameters depends on how many rows the SELECT statement returns.

  7. Release the memory that you allocated for the sqlvar_struct fields, the sqldata fields, and the sqlda structure itself with the free() system call.

Results

Important: If the SELECT statement has unknown columns in the select list, your program must also handle these columns with an sqlda structure. For more information, see Handling an unknown select list.