Declaring host variables and indicator variables in COBOL

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 must explicitly declare all host variables and host variable arrays that are used in SQL statements in the WORKING-STORAGE SECTION or LINKAGE SECTION of your program's DATA DIVISION.
    • You must explicitly declare each host variable and host variable array before using them in an SQL statement.
    • You can specify OCCURS when defining an indicator structure, a host variable array, or an indicator variable array. You cannot specify OCCURS for any other type of host variable.
    • You cannot implicitly declare any host variables through default typing or by using the IMPLICIT statement.
    • 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.
    • 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.