INSERT

The INSERT statement inserts rows into a table or view. Inserting a row into a view also inserts the row into the table on which the view is based.

There are three forms of this statement:

Invocation

This statement can be embedded in an application program or issued interactively. It is an executable statement that can be dynamically prepared with the exception of the n ROWS form, which must be a static statement embedded in an application program. The n ROWS form is not allowed in a REXX procedure.

Authorization

The privileges held by the authorization ID of the statement must include at least one of the following:

The authorization ID of the statement has the INSERT privilege on a table when:

The authorization ID of the statement has the INSERT privilege on a view when: 44

If a subselect is specified, the privileges held by the authorization ID of the statement must also include one of the following:

The authorization ID of the statement has the SELECT privilege on a table when:

The authorization ID of the statement has the SELECT privilege on a view when:

Syntax



>>-INSERT INTO----+-table-name-+-------------------------------->
                  '-view-name--'
 
>-----+----------------------------+---------------------------->
      |    .-,--------------.      |
      |    V                |      |
      '-(-----column-name---+---)--'
 
                      .-,--------------------------------------------------.
                      V                                                    |     (1)
>-----+--+-VALUES--(----+-constant--------------------------------------+--+---)-------+---+-------------------+-+>
      |  |              +-host-variable---------------------------------+              |   '-isolation-clause--' |
      |  |              +-special-register------------------------------+              |                         |
      |  |              +-NULL------------------------------------------+              |                         |
      |  |              +-DEFAULT---------------------------------------+              |                         |
      |  |              +-DLVALUE--(--arguments--)----------------------+              |                         |
      |  |              '-cast-function-name--(--+-constant------+---)--'              |                         |
      |  |                                       '-host-variable-'                     |                         |
      |  '-insert-multiple-rows--------------------------------------------------------'                         |
      '-+------------------+-------------------------------------------------------------------------------------'
        '-select-statement-'
 
>--------------------------------------------------------------><
 
insert-multiple-rows
 
|---+-integer-------+------------------------------------------->
    '-host-variable-'
 
>----ROWS--VALUES--(--host-structure-array--)-------------------|
 


Notes:


  1. If only one value is specified in the list, the parentheses around the
    value are optional.


Description

INTO table-name  or  view-name
Identifies the object of the insert operation. The name must identify a table or view that exists at the current server, but it must not identify a catalog table, a view of a catalog table, or a read-only view.

A value cannot be inserted into a view column that is derived from:

If the object of the insert operation is a view with such columns, a list of column names must be specified, and the list must not identify these columns.

(column-name,...)
Specifies the columns for which insert values are provided. Each name must be an unqualified name that identifies a column of the table or view. The same column must not be identified more than once. A view column that cannot accept insert values must not be identified.

Omission of the column list is an implicit specification of a list in which every column of the table or view is identified in left-to-right order. This list is established when the statement is prepared and, therefore, does not include columns that were added to a table after the statement was prepared.

If the INSERT statement is embedded in an application and the referenced table or view exists at create program time, the statement is prepared at create program time. Otherwise, the statement is prepared at the first successful execute of the INSERT statement.

VALUES
Specifies one new row in the form of a list of values.

The number of values in the VALUES clause must equal the number of names in the column list. The first value is inserted in the first column in the list, the second value in the second column, and so on.

constant
Specifies the value for a column is assigned a constant value. For an explanation of constant, see "Language Elements".

host-variable
Specifies the value for a column is assigned from a host variable.

Each host variable in the clause must identify a host structure or host variable that is declared in accordance with the rules for declaring host structures and host variables. In the operational form of the statement, a reference to a host structure is replaced by a reference to each of its variables. For an explanation of host-variable, see "Language Elements".

special-register
Specifies the value for a column is assigned from a special register. For a description of special-register, see Special Registers.

NULL
Specifies the value for a column is the null value. NULL should only be specified for nullable columns.

DEFAULT
Specifies that the default value is assigned to a column. The value that is inserted depends on how the column was defined, as follows:
  • If the WITH DEFAULT clause is used, the default inserted is as defined for the column (see default-clause in column-definition in CREATE TABLE).
  • If the WITH DEFAULT clause or the NOT NULL clause is not used, the value inserted is NULL.
  • If the NOT NULL clause is used and the WITH DEFAULT clause is not used or DEFAULT NULL is used, the DEFAULT keyword cannot be specified for that column.

DLVALUE(arguments)
Specifies the value for the column is the value resulting from a DLVALUE scalar function. A DLVALUE scalar function can only be specified for a DataLink column. The first argument of DLVALUE must be a constant, host variable, or a typed parameter marker (CAST(? AS data-type)). The second and third arguments of DLVALUE must be constants or host-variables.

cast-function-name
This form of an argument can only be used with columns defined as a distinct type, BLOB, CLOB, DBCLOB, DATE, TIME or TIMESTAMP data types. The following table describes the allowed uses of these cast-functions.
Column Data Type Cast Function Name
Distinct type N based on a BLOB, CLOB, or DBCLOB BLOB, CLOB, or DBCLOB *
Distinct type N based on a DATE, TIME, or TIMESTAMP DATE, TIME, or TIMESTAMP *
BLOB, CLOB, or DBCLOB BLOB, CLOB, or DBCLOB *
DATE, TIME, or TIMESTAMP DATE, TIME, or TIMESTAMP *
Notes:

* The name of the function must match the name of the data type (or the source type of the distinct type) with an implicit or explicit schema name of QSYS2

constant
Specifies a constant as the argument. The constant must conform to the rules of a constant for the source type of the distinct type or for the data type if not a distinct type. For BLOB, CLOB, DBCLOB, DATE, TIME, and TIMESTAMP functions, the constant must be a string constant.

host-variable
Specifies a host variable as the argument. The host variable must conform to the rules of a constant for the source type of the distinct type or for the data type if not a distinct type.

select-statement
Specifies a set of new rows in the form of the result table of a select-statement. The FOR READ ONLY, FOR UPDATE, and OPTIMIZE clauses are not valid for a select-statement used with insert. If an ORDER BY clause is specified on the select-statement, the rows are inserted according to the values of the columns identified in the ORDER BY clause.

The select-statement may produce values by combining two result tables with the UNION or UNION ALL operators. There can be one, more than one, or zero rows inserted when using the select-statement. If no rows are inserted, SQLCODE is set to +100 and SQLSTATE is set to '02000'.

When the base object of the INSERT and a base object of any subselect in the select statement are the same table, the select statement is completely evaluated before any rows are inserted.

The number of columns in the result table must equal the number of names in the column list. The value of the first column of the result is inserted in the first column in the list, the second value in the second column, and so on. For an explanation of select-statement, see "select-statement".

isolation-clause
Specifies the isolation level you want to use for the INSERT statement. For an explanation of isolation-clause, see "isolation-clause".

insert-multiple-rows

integer  or  host-variable ROWS

Specifies the number of rows to be inserted. If a host-variable is specified, it must be numeric with zero scale and cannot include an indicator variable.

VALUES (host-structure-array)

Specifies a set of new rows in the form of an array of host structures. The host-structure-array must be declared in the program in accordance with the rules for declaring host structure arrays. A parameter marker may be used in place of the host-structure-array name.

The number of variables in the host structure must equal the number of names in the column-list. The first host structure in the array corresponds to the first row, the second host structure in the array corresponds to the second row, and so on. In addition, the first variable in the host structure corresponds with the first column of the row, the second variable in the host structure corresponds with the second column of the row, and so on.

For an explanation of arrays of host structures see "Host Structure Arrays in C, C++, COBOL, PL/I, and RPG".

INSERT Rules

Default values
The value inserted in any column that is not in the column list is the default value of the column. Columns without a default value must be included in the column list. Similarly, if you insert into a view, the default value is inserted into any column of the base table that is not included in the view. Hence, all columns of the base table that are not in the view must have default values.

Assignment
Insert values are assigned to columns in accordance with the assignment rules described in "Language Elements"

Validity
If the identified table or the base table of the identified view has one or more unique indexes or unique constraints, each row inserted into the table must conform to the constraints imposed by those indexes.

The unique indexes and unique constraints are effectively checked at the end of the statement unless COMMIT(*NONE) was specified. In the case of a multiple-row insert, this would occur after all rows were inserted and any associated triggers were fired. If COMMIT(*NONE) is specified, checking is performed as each row is inserted.

If the identified table or the base table of the identified view has one or more check constraints, each check constraint must be true or unknown for each row inserted into the table.

The check constraints are effectively checked at the end of the statement. In the case of a multiple-row insert, this would occur after all rows were inserted.

If a view is identified, the inserted rows must conform to any applicable WITH CHECK OPTION. For more information, see "CREATE VIEW".

Triggers
If the identified table or the base table of the identified view has an insert trigger, the trigger is fired for each row inserted.

Referential Integrity
Each nonnull insert value of a foreign key must equal some value of the parent key of the parent table in the relationship.

The referential constraints (other than a referential constraint with a RESTRICT delete rule) are effectively checked at the end of the statement. In the case of a multiple-row insert, this would occur after all rows were inserted and any associated triggers were fired.

Notes

If an insert value violates any constraints, or if any other error occurs during the execution of an INSERT statement and COMMIT(*NONE) was not specified, all changes made during the execution of the statement are backed out. However, other changes in the unit of work made prior to the error are not backed out. If COMMIT(*NONE) is specified, changes are not backed out.

After executing an INSERT statement, the value of SQLERRD(3) of the SQLCA is the number of rows that the database manager inserted.

If COMMIT(*RR), COMMIT(*ALL), COMMIT(*CS), or COMMIT(*CHG) is specified, one or more exclusive locks are acquired during the execution of a successful INSERT statement. Until the locks are released by a commit or rollback operation, an inserted row can only be accessed by:

The locks can prevent other application processes from performing operations on the table. For further information about locking, see the description of the COMMIT, ROLLBACK, and LOCK TABLE statements. Also, see Isolation Level and the Database Programming book.

A maximum of 4000000 rows can be inserted or changed in any single INSERT statement when COMMIT(*RR), COMMIT(*ALL), COMMIT(*CS), or COMMIT(*CHG) was specified. The number of rows changed includes any rows inserted, updated, or deleted under the same commitment definition as a result of a trigger.

Host variables cannot be used in the INSERT statement within a REXX procedure. Instead, the INSERT must be the object of a PREPARE and EXECUTE using parameter markers.

Examples

Example 1

Insert a new department with the following specifications into the DEPARTMENT table:

   INSERT INTO DEPARTMENT
     VALUES ('E31', 'ARCHITECTURE', '00390', 'E01')

Example 2

Insert a new department into the DEPARTMENT table as in example 1, but do not assign a manager to the new department.

   INSERT INTO DEPARTMENT (DEPTNO, DEPTNAME, ADMRDEPT)
     VALUES ('E31', 'ARCHITECTURE', 'E01')

Example 3

Create a temporary table MA_EMP_ACT with the same columns as the EMP_ACT table. Load MA_EMP_ACT with the rows from the EMP_ACT table with a project number (PROJNO) starting with the letters 'MA'.

   CREATE TABLE MA_EMP_ACT
           (EMPNO     CHAR(6)  NOT NULL,
            PROJNO    CHAR(6)  NOT NULL,
            ACTNO     SMALLINT  NOT NULL,
            EMPTIME   DEC(5,2),
            EMSTDATE  DATE,
            EMENDATE  DATE )
   INSERT INTO MA_EMP_ACT
     SELECT * FROM EMP_ACT
       WHERE SUBSTR(PROJNO, 1, 2) = 'MA'

Example 4

Use a PL/I program statement to add a skeleton project to the PROJECT table. Obtain the project number (PROJNO), project name (PROJNAME), department number (DEPTNO), and responsible employee (RESPEMP) from host variables. Use the current date as the project start date (PRSTDATE). Assign a NULL value to the remaining columns in the table.

   EXEC SQL  INSERT INTO PROJECT  (PROJNO, PROJNAME, DEPTNO, RESPEMP, PRSTDATE)
               VALUES (:PRJNO, :PRJNM, :DPTNO, :REMP, CURRENT DATE);

Example 5

In a PL/I program, use a blocked insert to add 10 rows to table DEPARTMENT. The host structure array DEPT contains the data to be inserted.

   DCL  1  DEPT(10),
           3  DEPT  CHAR(3),
           3  LASTNAME  CHAR(29)  VARYING,
           3  WORKDEPT  CHAR(6),
           3  JOB  CHAR(3);
 
   EXEC SQL INSERT INTO CORPDATA.DEPARTMENT 10 ROWS VALUES (:DEPT);

Example 6

Insert a new project into the EMP_ACT table using the Read Uncommitted (UR, CHG) option:

   INSERT INTO EMP_ACT
     VALUES ('000140', 'PL2100', 30)
     WITH CHG


Footnotes:

44
When a view is created, the owner does not necessarily acquire the INSERT privilege on the view. The owner only acquires the INSERT privilege if the view allows inserts and the owner also has the INSERT privilege on the first table referenced in the subselect.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]