Handling an unknown select list

About this task

This section describes how to use a system-descriptor area to handle a SELECT statement.

To use a system-descriptor area to handle unknown select-list columns:

Procedure

  1. 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.
  2. Allocate a system-descriptor area with the ALLOCATE DESCRIPTOR statement.
  3. Determine the number and data types of the select-list columns with the DESCRIBE...USING SQL DESCRIPTOR statement.
    DESCRIBE fills an item descriptor for each column in the select list. For more information about DESCRIBE, see Initialize the system-descriptor area.
  4. Save the number of select-list columns in a host variable with the GET DESCRIPTOR statement to obtain the value of the COUNT field.
  5. Declare and open a cursor and then use the FETCH...USING SQL DESCRIPTOR statement to fetch column values, one row at a time, into an allocated system-descriptor area.
  6. Retrieve the row data from the system-descriptor area into host variables with the GET DESCRIPTOR statement to access the DATA field.
    For more information about GET DESCRIPTOR, see Assign and obtain values from a system-descriptor area.
  7. Deallocate the system-descriptor area with the DEALLOCATE DESCRIPTOR statement.

Results

Important: If the SELECT statement has unknown input parameters in the WHERE clause, your program must also handle these input parameters with a system-descriptor area.