Handling an unknown select list

About this task

For an introduction on how to handle unknown columns in an unknown select list, see Handling an unknown select list. This section describes how to use an sqlda structure to handle a SELECT statement.

To use an sqlda structure to handle unknown select-list columns:

Procedure

  1. Declare a variable to hold the address of an sqlda structure.
    For more information, see Define an sqlda structure.
  2. Prepare the SELECT statement (with the PREPARE statement) to give it a statement identifier.
    The SELECT statement cannot include an INTO TEMP clause. For more information, see Assemble and prepare the SQL statement.
  3. Use the DESCRIBE...INTO statement to perform two tasks:
    1. Allocate an sqlda structure.
      The address of the allocated structure is stored in the sqlda pointer that you declare. For more information, see Allocate memory for the sqlda structure.
    2. Determine the number and data types of select-list columns.
      The DESCRIBE statement fills an sqlvar_struct structure for each column of the select list. For more information, see Initialize the sqlda structure.
  4. Examine the sqltype and sqllen fields of sqlda for each select-list column to determine the amount of memory that is required to allocate for the data.
    For more information, see Allocate memory for column data.
  5. Save the number of select-list columns stored in the sqld field in a host variable.
  6. Declare and open a cursor and then use the FETCH...USING DESCRIPTOR statement to fetch column values, one row at a time, into an allocated sqlda structure.
  7. Retrieve the row data from the sqlda structure into host variables with C-language statements that access the sqldata field for each select-list column.
  8. Release memory allocated to the sqldata fields and the sqlda structure.
    For more information, see ids_esqlc_0656.html#ids_esqlc_0656.

Results

Important: If the SELECT statement has input parameters of an unknown number and type in the WHERE clause, your program must also handle these input parameters with an sqlda structure.

For more information, see Handling a parameterized SELECT statement.