Using host structures in C and C++ applications that use SQL

In C and C++ programs, you can define a host structure, which is a named set of elementary C or C++ variables.

Host structures have a maximum of two levels, even though the host structure might itself occur within a multilevel structure. An exception is the declaration of a varying-length string, which requires another structure.

A host structure name can be a group name whose subordinate levels name elementary C or C++ variables. For example:

   struct {
            struct {
                     char c1;
                     char c2;
                    } b_st;
          } a_st;

In this example, b_st is the name of a host structure consisting of the elementary items c1 and c2.

You can use the structure name as a shorthand notation for a list of scalars, but only for a two-level structure. You can qualify a host variable with a structure name (for example, structure.field). Host structures are limited to two levels. (For example, in the above host structure example, the a_st cannot be referred to in SQL.) A structure cannot contain an intermediate level structure. In the previous example, a_st could not be used as a host variable or referred to in an SQL statement. A host structure for SQL data has two levels and can be thought of as a named set of host variables. After the host structure is defined, you can refer to it in an SQL statement instead of listing the several host variables (that is, the names of the host variables that make up the host structure).

For example, you can retrieve all column values from selected rows of the table CORPDATA.EMPLOYEE with:

  struct { char empno[7];
                  struct           { short int firstname_len;
                                     char firstname_text[12];
                                   } firstname;
                  char midint,
                  struct           { short int lastname_len;
                                     char lastname_text[15];
                                   } lastname;
                  char workdept[4];
                  } pemp1;
    .....
  strcpy(pemp1.empno, "000220");
  .....
  exec sql
    SELECT *
      INTO :pemp1
      FROM corpdata.employee
      WHERE empno=:pemp1.empno;

Notice that in the declaration of pemp1, two varying-length string elements are included in the structure: firstname and lastname.