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 Informix® ESQL/C 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
- Declare a variable to hold the address of an sqlda structure.
For more information, see Define an sqlda structure.
- Determine the number and data types of the input parameters
of the SELECT statement.For more information, see Determine unknown input parameters.
- Allocate an sqlda structure with a system memory-allocation
function such as malloc(). For more information, see Specify input parameter values and Allocate memory for the sqlda structure.
- Indicate the number of input parameters in the WHERE clause with C-language statements that set the sqld field of the sqlda structure.
- 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 Informix® ESQL/C 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.
- 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.
- Release the memory that you allocated for the sqlvar_struct fields,
the sqldata fields, and the sqlda structure itself with
the free() system call.For more information, see Free memory allocated to an sqlda structure.