SQL/MP Programming Manual for COBOL

Data Retrieval and Modification
HP NonStop SQL/MP Programming Manual for COBOL529758-003
4-7
Inserting a Single Row
Inserting a Single Row
This INSERT statement inserts a row (JOBCODE and JOBDESC columns) into the
JOB table:
EXEC SQL BEGIN DECLARE SECTION;
* Declare host variables HV-JOBCODE and HV-JOBDESC.
...
EXEC SQL END DECLARE SECTION;
...
PROCEDURE DIVISION.
...
* Move values to HV-JOBCODE and HV-JOBDESC.
...
EXEC SQL INSERT INTO PERSNL.JOB (JOBCODE, JOBDESC)
VALUES (:HV-JOBCODE, :HV-JOBDESC)
END-EXEC.
...
If the INSERT operation fails, check for SQL error 8227, which indicates you attempted
to insert a row with an existing key (primary or unique alternate).
Inserting a Null Value
This example inserts a row into the EMPLOYEE table and sets the SALARY column to
a null value using an indicator variable:
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
* Declare host variables EMPNUM, FIRST-NAME,
* LAST-NAME, DEPTNUM, JOBCODE, and SALARY.
01 IND-1 PIC S9(4) COMP.
...
EXEC SQL END DECLARE SECTION END-EXEC.
...
PROCEDURE DIVISION.
...
MOVE -1 TO IND-1.
* Move values to host variables EMPNUM, FIRST-NAME,
* LAST-NAME, DEPTNUM, JOBCODE, and SALARY.
...
EXEC SQL INSERT INTO PERSNL.EMPLOYEE
VALUES (:EMPNUM, :FIRST-NAME, :LAST-NAME,
:DEPTNUM,:JOBCODE,
:SALARY INDICATOR :IND-1)
END-EXEC.
This example uses the NULL keyword instead of an indicator variable:
EXEC SQL INSERT INTO PERSNL.EMPLOYEE
VALUES (:EMPNUM, :FIRST-NAME, :LAST-NAME,
:DEPTNUM,:JOBCODE, NULL)
END-EXEC.