Host structure support in the declare section of C and C++ embedded SQL applications
Host structure support provides a shorthand for referencing that same set of host variables in an SQL statement.
For example, the following host structure can be used to access some of the columns in the STAFF table of the SAMPLE database:
struct tag
{
short id;
struct
{
short length;
char data[10];
} name;
struct
{
short years;
double salary;
} info;
} staff_record;
The fields of a host structure can be any of the valid host variable types. Valid types include
all numeric, character, and large object types. Nested host structures are also supported up to 25
levels. In the example shown previously, the field info is a sub-structure, whereas
the field name is not, as it represents a VARCHAR field. The same principle applies
to LONG VARCHAR, VARGRAPHIC and LONG VARGRAPHIC.
Pointer to host structure is only
supported when it is not nested within another structure.
- The host structure name can be referenced in an SQL statement.
The precompiler converts the reference toEXEC SQL SELECT id, name, years, salary INTO :staff_record FROM staff WHERE id = 10;staff_recordinto a list, separated by commas, of all the fields declared within the host structure. Each field is qualified with the host structure names of all levels to prevent naming conflicts with other host variables or fields. This is equivalent to the following method. - Fully qualified host variable names can be referenced in an SQL
statement.
References to field names must be fully qualified, even if there are no other host variables with the same name. Qualified sub-structures can also be referenced. In the preceding example,EXEC SQL SELECT id, name, years, salary INTO :staff_record.id, :staff_record.name, :staff_record.info.years, :staff_record.info.salary FROM staff WHERE id = 10;:staff_record.infocan be used to replace:staff_record.info.years, :staff_record.info.salary.
EXEC SQL DELETE FROM :staff_record;Here,
the DELETE statement expects a single character-based host variable.
By giving a host structure instead, the statement results in a precompile-time
error: SQL0087N Host variable "staff_record" is a structure used where structure
references are not permitted.Other uses of host structures, which can cause an SQL0087N error to occur, include PREPARE, EXECUTE IMMEDIATE, CALL, indicator variables and SQLDA references. Host structures with exactly one field are permitted in such situations, as are references to individual fields (second example).