Previous Topic: Retrieving DataNext Topic: Modifying Data


Adding Data

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:

Checking INSERT Status

Since the DBMS enforces integrity constraints, the program can test SQLCERC for a constraint violation:

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.