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
- 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.
- Allocate a system-descriptor area with the ALLOCATE DESCRIPTOR
statement.For more information, see Allocate memory for a system-descriptor area.
- 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.
- Save the number of select-list columns in a host variable with the GET DESCRIPTOR statement to obtain the value of the COUNT field.
- 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.
- 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.
- Deallocate the system-descriptor area with the DEALLOCATE
DESCRIPTOR statement.For more information, see Free memory allocated to a system-descriptor area.
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.