Handling an unknown column list
For an introduction on how to handle columns in a VALUES clause of an INSERT, see Handling an unknown column list. This section describes how to use a system-descriptor area to handle the INSERT...VALUES statement.
To use a system-descriptor area to handle input parameters in an INSERT:
- Prepare the INSERT statement (with the PREPARE statement) to give it a statement identifier. 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 columns with the DESCRIBE...USING SQL DESCRIPTOR statement. The DESCRIBE statement 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 unknown columns in a host variable with the GET DESCRIPTOR statement, which obtains the value of the COUNT field.
- Set the columns to their values with the SET DESCRIPTOR statement, which sets the appropriate DATA and VALUE fields. The column values must be compatible with the data type of their associated column. If you want to insert a NULL value, set the INDICATOR field to -1, and do not specify any DATA field in the SET DESCRIPTOR statement. For more information about SET DESCRIPTOR, see Assign and obtain values from a system-descriptor area.
- Execute the INSERT statement to insert the values into the database.
The following sections demonstrate how to execute a simple INSERT statement that inserts only one row and one that uses an insert cursor to insert several rows from an insert buffer.
- Deallocate the system-descriptor area with the DEALLOCATE DESCRIPTOR statement. See Free memory allocated to a system-descriptor area.