Using INSERT
In a program, you use an INSERT statement to add data to the database in one of the following ways:
Single-row INSERT
To add a single row to a table, issue an INSERT statement with a VALUES clause that specifies a value for each column in the column list:
EXEC SQL INSERT INTO DIVISION (DIV_CODE, DIV_NAME) ◄─── Column list VALUES (:DIV-CODE, :DIV-NAME) END-EXEC.
Multiple-row INSERT with SELECT
One way to add multiple rows to a table is to insert the result table of a SELECT statement.
In this example, a result table of data from the EMPLOYEE table is inserted into a table named TEMP_MGR:
EXEC SQL INSERT INTO TEMP_MGR SELECT DISTINCT E.MANAGER_ID, M.EMP_FNAME, M.EMP_LNAME FROM EMPLOYEE E, EMPLOYEE M WHERE E.MANAGER_ID = M.EMP_ID END-EXEC.
Guidelines for INSERT
Apply these guidelines when formulating an INSERT statement:
EXEC SQL INSERT INTO DIVISION VALUES ('D06', 'ADVANCED RESEARCH', NULL) -- Division head id is null -- END-EXEC.
When embedding an INSERT statement with a VALUES clause, you should include a column list even if you mean to insert values into all columns. Using a VALUES clause but omitting a column list can cause a program error if, for example, a column has been added to the table.
If an INSERT statement omits a table column from the column list, the DBMS:
Checking INSERT Status
Since the DBMS enforces integrity constraints, the program can test SQLCERC for a constraint violation:
Note: Referential constraints defined as linked clustered are not permitted to not cross page group boundaries.
Here is an example for a specific test for a check constraint violation:
IF SQLCERC = 1023 PERFORM INVALID-DATA ELSE IF SQLCODE < 0 GOTO ERROR-ROUTINE.
If an INSERT statement that uses a SELECT statement executes successfully but adds no rows, the DBMS returns 100 to SQLCODE and 0 to SQLCNRP.
Inserting Multiple Rows
You can add a set of rows to a table using one INSERT statement with a BULK clause.
Note: For more information about using bulk processing to insert, see Bulk Processing.
Copyright © 2013 CA.
All rights reserved.
|
|