SQL/MP Programming Manual for COBOL

Host Variables
HP NonStop SQL/MP Programming Manual for COBOL529758-003
2-12
Testing for a Null Value
02 RETIRE-IND PIC S9(4) COMP.
EXEC SQL END DECLARE SECTION END-EXEC.
...
PROCEDURE DIVISION.
...
MOVE NULL-EMPNUM TO EMPNUM.
MOVE -1 TO RETIRE-IND.
EXEC SQL
INSERT INTO =RETIREES
VALUES (:EMPNUM,:RETIRE-DATE INDICATOR :RETIRE-IND)
END-EXEC.
...
The next example uses the NULL keyword instead of an indicator variable to insert the
null value:
MOVE NULL-EMPNUM TO EMPNUM.
...
EXEC SQL
INSERT INTO =RETIREES VALUES (:EMPNUM, NULL)
END-EXEC.
Testing for a Null Value
To test for a null value, a program tests the indicator variable associated with a host
variable. This example selects data from the PRODUCTS table and then tests for a null
value using the indicator variable SHIP-IND. After the SELECT statement executes,
the example tests the indicator variable for a null value. If the value of the indicator
variable is less than 0, the associated column contains a null value.
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 PRODUCT-REC.
02 PRODNUM PIC 9(5) COMP.
02 DATE-SHIPPED PIC X(10).
02 SHIP-IND PIC 9(4) COMP.
...
EXEC SQL END DECLARE SECTION END-EXEC.
* Variable for displaying the date or NULL:
01 VALUE-DISPLAY PIC X(10) VALUE SPACES.
...
* Declare a cursor to perform the SELECT:
EXEC SQL DECLARE GET-PRODNUM CURSOR FOR
SELECT PRODNUM, DATE-SHIPPED FROM =PRODUCTS
WHERE PRODNUM > MAX-PRODNUM
END-EXEC.
PROCEDURE DIVISION.
0100-MAIN.
...
EXEC SQL OPEN GET-PRODNUM END-EXEC.
PERFORM 0150-SELECT UNTIL SQLCODE OF SQLCA NOT = 0.
EXEC SQL CLOSE GET-PRODNUM END-EXEC.