DB2 Version 9.7 for Linux, UNIX, and Windows

INSERT statement

The INSERT statement inserts rows into a table, nickname, or view, or the underlying tables, nicknames, or views of the specified fullselect. Inserting a row into a nickname inserts the row into the data source object to which the nickname refers. Inserting a row into a view also inserts the row into the table on which the view is based, if no INSTEAD OF trigger is defined for the insert operation on this view. If such a trigger is defined, the trigger will be executed instead.

Invocation

This statement can be embedded in an application program or issued through the use of dynamic SQL statements. It is an executable statement that can be dynamically prepared.

Authorization

The privileges held by the authorization ID of the statement must include at least one of the following:
  • INSERT privilege on the target table, view, or nickname
  • CONTROL privilege on the target table, view, or nickname
  • DATAACCESS authority
In addition, for each table, view, or nickname referenced in any fullselect used in the INSERT statement, the privileges held by the authorization ID of the statement must include at least one of the following:
  • SELECT privilege
  • CONTROL privilege
  • DATAACCESS authority

GROUP privileges are not checked for static INSERT statements.

If the target of the insert operation is a nickname, the privileges on the object at the data source are not considered until the statement is executed at the data source. At this time, the authorization ID that is used to connect to the data source must have the privileges required for the operation on the object at the data source. The authorization ID of the statement can be mapped to a different authorization ID at the data source.

Syntax

Read syntax diagramSkip visual syntax diagram
>>-INSERT INTO--+-table-name-------+---------------------------->
                +-view-name--------+   
                +-nickname---------+   
                '-(--fullselect--)-'   

>--+-----------------------+--+---------------------+----------->
   |    .-,-----------.    |  '-| include-columns |-'   
   |    V             |    |                            
   '-(----column-name-+--)-'                            

             .-,----------------------------.              
             V                              |              
>--+-VALUES----+-+-expression-+-----------+-+----------+-------->
   |           | +-NULL-------+           |            |   
   |           | '-DEFAULT----'           |            |   
   |           |    .-,--------------.    |            |   
   |           |    V                |    |            |   
   |           +-(----+-expression-+-+--)-+            |   
   |           |      +-NULL-------+      |            |   
   |           |      '-DEFAULT----'      |            |   
   |           '-row-expression-----------'            |   
   '-+-----------------------------------+--fullselect-'   
     |       .-,-----------------------. |                 
     |       V                         | |                 
     '-WITH----common-table-expression-+-'                 

>--+--------------+--------------------------------------------><
   '-WITH--+-RR-+-'   
           +-RS-+     
           +-CS-+     
           '-UR-'     

include-columns

               .-,----------------------.      
               V                        |      
|--INCLUDE--(----column-name--data-type-+--)--------------------|

Description

INTO table-name, view-name, nickname, or (fullselect)
Identifies the object of the insert operation. The name must identify a table, view or nickname that exists at the application server, but it must not identify a catalog table, a system-maintained materialized query table, a view of a catalog table, or a read-only view, unless an INSTEAD OF trigger is defined for the insert operation on the subject view. Rows inserted into a nickname are placed in the data source object to which the nickname refers.

If the object of the insert operation is a fullselect, the fullselect must be insertable, as defined in the "Insertable views" Notes item in the description of the CREATE VIEW statement.

If the object of the insert operation is a nickname, the extended indicator variable values of DEFAULT and UNASSIGNED must not be used (SQLSTATE 22539).

If no INSTEAD OF trigger exists for the insert operation on this view, a value cannot be inserted into a view column that is derived from:
  • A constant, expression, or scalar function
  • The same base table column as some other column of the view

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.

A row can be inserted into a view or a fullselect that is defined using a UNION ALL if the row satisfies the check constraints of exactly one of the underlying base tables. If a row satisfies the check constraints of more than one table, or no table at all, an error is returned (SQLSTATE 23513).

A row cannot be inserted into a view or a fullselect that is defined using a UNION ALL if any base table of the view contains a before trigger and the before trigger contains an UPDATE, a DELETE, or an INSERT operation, or invokes any routine containing such operations (SQLSTATE 42987).

(column-name,...)
Specifies the columns for which insert values are provided. Each name must identify a column of the specified table, view, or nickname, or a column in the fullselect. The same column must not be identified more than once. If extended indicator variables are not enabled, a column that cannot accept inserted values (for example, a column based on an expression) must not be identified.

Omission of the column list is an implicit specification of a list in which every column of the table (that is not implicitly hidden) or view, or every item in the select-list of the fullselect 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.

include-columns
Specifies a set of columns that are included, along with the columns of table-name or view-name, in the intermediate result table of the INSERT statement when it is nested in the FROM clause of a fullselect. The include-columns are appended at the end of the list of columns that are specified for table-name or view-name.
INCLUDE
Specifies a list of columns to be included in the intermediate result table of the INSERT statement. This clause can only be specified if the INSERT statement is nested in the FROM clause of a fullselect.
column-name
Specifies a column of the intermediate result table of the INSERT statement. The name cannot be the same as the name of another include column or a column in table-name or view-name (SQLSTATE 42711).
data-type
Specifies the data type of the include column. The data type must be one that is supported by the CREATE TABLE statement.
VALUES
Introduces one or more rows of values to be inserted.

Each row specified in the VALUES clause must be assignable to the implicit or explicit column list and the columns identified in the INCLUDE clause, unless a row variable is used. When a row value list in parentheses is specified, the first value is inserted into the first column in the list, the second value into the second column, and so on. When a row expression is specified, the number of fields in the row type must match the number of names in the implicit or explicit column list.

expression
An expression can be any expression defined in the "Expressions" topic. If expression is a row type, it must not appear in parentheses. If expression is a variable, the host variable can include an indicator variable or in the case of a host structure, an indicator array, enabled for extended indicator variables. If extended indicator variables are enabled, the extended indicator variable values of default (-5) or unassigned (-7) must not be used (SQLSTATE 22539) if either of the following statements is true:
  • The expression is more complex than a single host variable with explicit casts
  • The target column has data type of structured type
NULL
Specifies the null value and should only be specified for nullable columns.
DEFAULT
Specifies that the default value is to be used. The result of specifying DEFAULT depends on how the column was defined, as follows:
  • If the column was defined as a generated column based on an expression, the column value is generated by the system, based on that expression.
  • If the IDENTITY clause is used, the value is generated by the database manager.
  • If the ROW CHANGE TIMESTAMP clause is used, the value for each inserted row is generated by the database manager as a timestamp that is unique for the table partition within the database partition.
  • If the WITH DEFAULT clause is used, the value inserted is as defined for the column (see default-clause in "CREATE TABLE").
  • If the NOT NULL clause is used and the GENERATED clause is not used, or the WITH DEFAULT clause is not used or DEFAULT NULL is used, the DEFAULT keyword cannot be specified for that column (SQLSTATE 23502).
  • When inserting into a nickname, the DEFAULT keyword will be passed through the INSERT statement to the data source only if the data source supports the DEFAULT keyword in its query language syntax.
row-expression
Specifies any row expression of the type described in "Row expressions" that does not include a column name. The number of fields in the row must match the target of the insert and each field must be assignable to the corresponding column.
WITH common-table-expression
Defines a common table expression for use with the fullselect that follows.
fullselect
Specifies a set of new rows in the form of the result table of a fullselect. There may be one, more than one, or none. If the result table is empty, SQLCODE is set to +100 and SQLSTATE is set to '02000'.

When the base object of the INSERT and the base object of the fullselect or any subquery of the fullselect, are the same table, the fullselect 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.

If the expression that specifies the value of a result column is a variable, the host variable can include an indicator variable enabled for extended indicator variables. If extended indicator variables are enabled, and the expression is more than a single host variable, or a host variable being explicitly cast, then the extended indicator variable values of default or unassigned must not be used (SQLSTATE 22539). The effects of default or unassigned values apply to the corresponding target columns of the fullselect.

WITH
Specifies the isolation level at which the fullselect is executed.
RR
Repeatable Read
RS
Read Stability
CS
Cursor Stability
UR
Uncommitted Read
The default isolation level of the statement is the isolation level of the package in which the statement is bound. The WITH clause has no effect on nicknames, which always use the default isolation level of the statement.

Rules

Notes

Examples

Example 1:  Insert a new department with the following specifications into the DEPARTMENT table:
  • Department number (DEPTNO) is 'E31'
  • Department name (DEPTNAME) is 'ARCHITECTURE'
  • Managed by (MGRNO) a person with number '00390'
  • Reports to (ADMRDEPT) department 'E01'.
   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:  Insert two new departments using one statement into the DEPARTMENT table as in example 2, but do not assign a manager to the new department.
   INSERT INTO DEPARTMENT (DEPTNO, DEPTNAME, ADMRDEPT)
     VALUES ('B11', 'PURCHASING', 'B01'),
            ('E41', 'DATABASE ADMINISTRATION', 'E01')
Example 4:  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 5:  Use a C 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 6:  Specify an INSERT statement as the data-change-table-reference within a SELECT statement. Define an extra include column whose values are specified in the VALUES clause, which is then used as an ordering column for the inserted rows.
   SELECT INORDER.ORDERNUM
     FROM NEW TABLE (INSERT INTO ORDERS(CUSTNO)INCLUDE (INSERTNUM INTEGER)
       VALUES(:CNUM1, 1), (:CNUM2, 2)) InsertedOrders
     ORDER BY INSERTNUM;
Example 7: Use a C program statement to add a document to the DOCUMENTS table. Obtain values for the document ID (DOCID) column and the document data (XMLDOC) column from a host variable that binds to an SQL TYPE IS XML AS BLOB_FILE.
   EXEC SQL INSERT INTO DOCUMENTS
     (DOCID, XMLDOC) VALUES (:docid, :xmldoc)
Example 8: For the following INSERT statements, assume that table SALARY_INFO is defined with three columns, and that the last column is an implicitly hidden ROW CHANGE TIMESTAMP column. In the following statement, the implicitly hidden column is explicitly referenced in the column list and a value is provided for it in the VALUES clause.
   INSERT INTO SALARY_INFO (LEVEL, SALARY, UPDATE_TIME)
     VALUES (2, 30000, CURRENT TIMESTAMP)
The following INSERT statement uses an implicit column list. An implicit column list does not include implicitly hidden columns, so the VALUES clause only contains values for the other two columns.
   INSERT INTO SALARY_INFO VALUES (2, 30000)
In this case, the UPDATE_TIME column must be defined to have a default value, and that default value is used for the row that is inserted.