SQL/MX Programming Manual for C and COBOL (G06.24+, H06.03+)
Host Variables in COBOL Programs
HP NonStop SQL/MX Programming Manual for C and COBOL—523627-004
4-22
Creating COBOL Host Variables Using INVOKE
Example
To retrieve rows that have null salaries from the EMPLOYEE table using a cursor, 
specify the NULL predicate in the WHERE clause in the associated SELECT statement 
when you declare the cursor: 
* Declare a cursor to find rows with null salaries. 
 EXEC SQL DECLARE get_null_salary CURSOR FOR
 SELECT empnum, first_name, last_name, 
 deptnum, jobcode, salary 
 FROM employee
 WHERE salary IS NULL 
 END-EXEC. 
 ... 
 PROCEDURE DIVISION.
 0100-MAIN.
 ... 
 EXEC SQL OPEN get_null_salary END-EXEC.
 PERFORM 200-FETCH-NULL UNTIL SQLSTATE = "02000".
 EXEC SQL CLOSE get_null_salary END-EXEC.
 ... 
 0200-FETCH-NULL.
 EXEC SQL FETCH get_null_salary INTO
 :EMPNUM OF EMPLOYEE-RECORD,
 :FIRST-NAME OF EMPLOYEE-RECORD 
 :LAST-NAME OF EMPLOYEE-RECORD 
 :DEPTNUM OF EMPLOYEE-RECORD 
 :JOBCODE OF EMPLOYEE-RECORD 
 :SALARY OF EMPLOYEE-RECORD 
 END-EXEC. 
* Process the row that contains the null salary.
 ... 
Creating COBOL Host Variables Using INVOKE
The INVOKE preprocessor directive creates host variables corresponding to columns 
in a table or view. INVOKE converts the column names to COBOL names and 
generates a COBOL data item for each column. If a column allows null, INVOKE also 
creates an indicator variable for the column. 
You can declare host variables that correspond to the columns in an SQL table or view 
without using an INVOKE statement. However, using an INVOKE statement to 
generate host variables has these advantages: 
•
Program independence: If you modify a table or view, the INVOKE statement re-
creates the host variables to correspond to the new table or view when you run the 
SQL/MX COBOL preprocessor. However, you must modify a program that refers to 
a deleted column or accesses a new column.
•
Performance: The INVOKE statement maps SQL data types to the corresponding 
host language data types, and usually no data conversion is required at run time. 
For further information, see Example 4-2 on page 4-24 and Example 4-3 on 
page 4-25.
COBOL










