Declaring unions and structures

You can declare unions and structures, either directly or by using a type definition (typedef). You can use an element of a union as a host variable, but not the union as a whole. In contrast, a host variable can be either an entire structure or just one of the structure’s elements. The following example declares a union and a structure:

exec sql begin declare section;
     typedef int PAYMENT_METHOD;
     PAYMENT_METHOD method;
     union salary_or_percentage {
         CS_MONEY salary;
         CS_NUMERIC percentage;
         } amount;
      struct employee_record {
         char first_name[30];
         char last_name[30];
         char employee_ID[30];
         } this_employee;
      char *employee_of_the_month_ID = "01234567";
 exec sql end declare section;
 
 exec sql select first_name, last_name, employee_ID
     into :this_employee
     from employee_table
     where employee_ID = :employee_of_the_month_ID;
 exec sql select payment_type into :method
     from remuneration_table where employee_ID =
     :this_employee.employee_ID;
 switch (method) {
     case SALARIED:
         exec sql select salary into
         :amount.salary
         from remuneration_table
         where employee_ID =
         this_employee.employee_ID;
         break;
     case VOLUNTEER:
         exec sql select 0 into
         :amount.salary
         break;
     case COMMISSION:
         exec sql select commission_percentage into
         :amount.percentage
         from remuneration_table
         where employee_ID =
         this_employee.employee_ID;
         break;
     }