Declaring host variables and indicator variables in C

You can use host variables, host variable arrays, and host structures in SQL statements in your program to pass data between DB2® and your application.

Procedure

To declare host variables, host variable arrays, and host structures:

  1. Declare the variables according to the following rules and guidelines:
    • You can have more than one host variable declaration section in your program.
    • You can use class members as host variables. Class members that are used as host variables are accessible to any SQL statement within the class. However, you cannot use class objects as host variables.
    • If you specify the ONEPASS SQL processing option, you must explicitly declare each host variable and each host variable array before using them in an SQL statement. If you specify the TWOPASS precompiler option, you must declare each host variable before using it in the DECLARE CURSOR statement.
      Restriction: The DB2 coprocessor for C/C++ supports only the ONEPASS option.
    • If you specify the STDSQL(YES) SQL processing option, you must precede the host language statements that define the host variables and host variable arrays with the BEGIN DECLARE SECTION statement and follow the host language statements with the END DECLARE SECTION statement. Otherwise, these statements are optional.
    • Ensure that any SQL statement that uses a host variable or host variable array is within the scope of the statement that declares that variable or array.
    • If you are using the DB2 precompiler, ensure that the names of host variables and host variable arrays are unique within the program, even if the variables and variable arrays are in different blocks, classes, procedures, functions, or subroutines. You can qualify the names with a structure name to make them unique.
  2. Optional: Define any associated indicator variables, arrays, and structures.