Initialize the sqlda structure
To send or receive column values in the database, your program must initialize the sqlda structure so that it describes the unknown columns of the prepared statement.
- Set the sqlvar field to the address of the initialized sqlvar_struct structures.
- Set the sqld field to indicate the number of unknown columns (and associated sqlvar_struct structures).
In addition to allocating memory for the sqlda structure (see Allocate memory for the sqlda structure), the DESCRIBE...INTO statement also initializes this structure with information about the prepared statement. The information that DESCRIBE...INTO can provide depends on which SQL statement it has described.
- It allocates memory for the sqlda structure. For more information, see Allocate memory for the sqlda structure.
- It sets the sqlda.sqld field, which contains the number of sqlvar_struct structures initialized with data. This value is the number of columns and expressions in the column list (SELECT and INSERT) or the number of returned values (EXECUTE FUNCTION).
- It allocates memory for component sqlvar_struct structures, one sqlvar_struct structure for each column or expression in the column list (SELECT and INSERT) or for each of the returned values (EXECUTE FUNCTION).
- It sets the sqlda.sqlvar field to the initial address of the memory that DESCRIBE allocates for the sqlvar_struct structures.
- It describes each unknown column in the prepared SELECT (without
an INTO TEMP), EXECUTE FUNCTION, or INSERT statement. The DESCRIBE...INTO
statement initializes the fields of the sqlvar_struct structure
for each column, as follows:
- It initializes the sqltype, sqllen, and sqlname fields
(for CHAR type data or for the qualifier of DATETIME or INTERVAL data)
to provide information from the database about the column.
For most data types, the sqllen field holds the length, in bytes, of the column. If the column is a collection type (SET, MULTISET, or LIST), a row type (named or unnamed), or an opaque type, the sqllen field is zero.
- It initializes the sqldata and sqlind fields to null.
For descriptions of these fields, see Fields in the sqlvar_struct structure.
- It initializes the sqltype, sqllen, and sqlname fields
(for CHAR type data or for the qualifier of DATETIME or INTERVAL data)
to provide information from the database about the column.
For more information, see Allocate memory for column data.
The DESCRIBE statement provides information about the columns of a column list. Therefore, you usually use DESCRIBE...INTO after a SELECT (without an INTO TEMP clause), INSERT, or EXECUTE FUNCTION statement was prepared. The DESCRIBE...INTO statement not only initializes the sqlda structure, but also returns the type of SQL statement prepared. For more information, see Determine the statement type.
EXEC SQL prepare st_id
'select customer_num, company from customer
where customer_num = ?';
EXEC SQL describe st_id into da_ptr;
The preceding DESCRIBE...INTO statement returns an SQLCODE
value of 0
to indicate that the prepared statement
was a SELECT statement.
- Allocate memory for component sqlvar_struct structures,
one sqlvar_struct structure for each column.You can use system memory-allocation functions such as malloc() or calloc() and assign the address to sqlvar, as follows:
da_ptr->sqlvar = (struct sqlvar_struct *) calloc(count, sizeof(struct sqlvar_struct));
- Perform the following tasks to describe each unknown column:
- Set the sqlda.sqld field, which contains the number of sqlvar_struct structures initialized with data. This value is the number of unknown columns in the prepared statement.
- Initialize the fields of each sqlvar_struct structure.
Set the sqltype, sqllen, and sqlname fields (for CHAR type data or for the qualifier for DATETIME or INTERVAL data) to provide information about a column to the database server.
To provide the column data, your program must also allocate space for this data and set the sqldata field of each sqlvar_struct structure to the appropriate location within this space. For more information, see Allocate memory for column data. If you send column data to the database server, be sure to set the sqlind field appropriately.
If you use the sqlda structure to define input parameters, you cannot use a DESCRIBE statement to initialize the sqlda structure. Your code must explicitly set the appropriate fields of the sqlda structure to define the input parameters. (See Specify input parameter values.)