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 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.
- Determine the number and data types of the input parameters
of the SELECT statement.
- Allocate an sqlda structure with a system memory-allocation
function such as malloc().
- 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 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.
Results
Important: If the SELECT statement has unknown
columns in the select list, your program must also handle these columns
with an
sqlda structure. For more information, see
Handling an unknown select list.