SQL Reference

UPDATE

The UPDATE statement updates the values of specified columns in rows of a table or view. Updating a row of a view updates a row of its base table.

The forms of this statement are:

Invocation

An UPDATE 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:

For each table or view referenced by a subquery, the privileges held by the authorization ID of the statement must also include at least one of the following:

When the package is precompiled with SQL92 rules 107 and the searched form of an UPDATE includes a reference to a column of the table or view in the right side of the assignment-clause or anywhere in the search-condition, the privileges held by the authorization ID of the statement must also include at least one of the following:

When the specified table or view is preceded by the ONLY keyword, the privileges held by the authorization ID of the statement must also include the SELECT privilege for every subtable or subview of the specified table or view.

GROUP privileges are not checked for static UPDATE statements.

Syntax

Searched UPDATE:

>>-UPDATE----+-table-name-------------------+------------------->
             +-view-name--------------------+
             '-ONLY--(--+-table-name-+---)--'
                        '-view-name--'
 
>-----+---------------------------+----------------------------->
      | .-AS-.                    |
      '-+----+--correlation-name--'
 
>-----SET--| assignment-clause |-------------------------------->
 
>-----+--------------------------+-----------------------------><
      '-WHERE--search-condition--'
 

Positioned UPDATE:

>>-UPDATE----+-table-name-------------------+------------------->
             +-view-name--------------------+
             '-ONLY--(--+-table-name-+---)--'
                        '-view-name--'
 
>-----SET--| assignment-clause |-------------------------------->
 
>-----WHERE CURRENT OF--cursor-name----------------------------><
 
assignment-clause
 
    .-,---------------------------------------------------------------------------------------------------------.
    V                                                                                                           |
|-------+-column-name--+--------------------------+---=--+-expression-+--------------------------------------+--+->
        |              |  .--------------------.  |      +-NULL-------+                                      |
        |              |  V                    |  |      '-DEFAULT----'                                      |
        |              '----..attribute-name---+--'                                                          |
        |    .-,-------------------------------------------.               .-,----------------------.        |
        |    V                                             |               V                 (1)    |        |
        '-(-----column-name--+--------------------------+--+---)--=--(--+----+-expression-+---------+-+---)--'
                             |  .--------------------.  |               |    +-NULL-------+           |
                             |  V                    |  |               |    '-DEFAULT----'           |
                             '----..attribute-name---+--'               |                (2)          |
                                                                        '-row-fullselect--------------'
 
>---------------------------------------------------------------|
 

Notes:

  1. The number of expressions, NULLs and DEFAULTs must match the number of column-names.

  2. The number of columns in the select list must match the number of column-names.

Description

table-name  or  view-name
Is the name of the table or view to be updated. The name must identify a table or view described in the catalog, but not a catalog table, a view of a catalog table (unless it is one of the updatable SYSSTAT views), a summary table, read-only view, or a nickname. (For an explanation of read-only views, see CREATE VIEW. For an explanation of updatable catalog views, see Appendix D, Catalog Views.)

If table-name is a typed table, rows of the table or any of its proper subtables may get updated by the statement. Only the columns of the specified table may be set or referenced in the WHERE clause. For a positioned UPDATE, the associated cursor must also have specified the same table or view in the FROM clause without using ONLY.

ONLY (table-name)
Applicable to typed tables, the ONLY keyword specifies that the statement should apply only to data of the specified table and rows of proper subtables cannot be updated by the statement. For a positioned UPDATE, the associated cursor must also have specified the table in the FROM clause using ONLY. If table-name is not a typed table, the ONLY keyword has no effect on the statement.

ONLY (view-name)
Applicable to typed views, the ONLY keyword specifies that the statement should apply only to data of the specified view and rows of proper subviews cannot be updated by the statement. For a positioned UPDATE, the associated cursor must also have specified the view in the FROM clause using ONLY. If view-name is not a typed view, the ONLY keyword has no effect on the statement.

AS
Optional keyword to introduce the correlation-name.

correlation-name
May be used within search-condition to designate the table or view. (For an explanation of correlation-name, see Correlation Names.)

SET
Introduces the assignment of values to column names.

assignment-clause

column-name
Identifies a column to be updated. The column-name must identify an updatable column of the specified table or view. 108 The object ID column of a typed table is not updatable (SQLSTATE 428DZ). A column must not be specified more than once, unless it is followed by an attribute-name (SQLSTATE 42701).

For a Positioned UPDATE:

  • If the UPDATE clause was specified in the select-statement of the cursor, each column name in the assignment-clause must also appear in the UPDATE clause.
  • If the UPDATE clause was not specified in the select-statement of the cursor and LANGLEVEL MIA or SQL92E was specified when the application was precompiled, the name of any updatable column may be specified.
  • If the UPDATE clause was not specified in the select-statement of the cursor and LANGLEVEL SAA1 was specified either explicitly or by default when the application was precompiled, no columns may be updated.

..attribute-name
Specifies the attribute of a structured type that is set (referred to as an attribute assignment. The column-name specified must be defined with a user-defined structured type (SQLSTATE 428DP). The attribute-name must be an attribute of the structured type of column-name (SQLSTATE 42703). An assignment that does not involve the ..attribute-name clause is referred to as a conventional assignment.

expression
Indicates the new value of the column. The expression is any expression of the type described in Expressions. The expression can not include a column function except when it occurs within a scalar fullselect (SQLSTATE 42903).

An expression may contain references to columns of the target table of the UPDATE statement. For each row that is updated, the value of such a column in an expression is the value of the column in the row before the row is updated.

NULL
Specifies the null value and can only be specified for nullable columns (SQLSTATE 23502). NULL cannot be the value in an attribute assignment (SQLSTATE 429B9) unless it is specifically cast to the data type of the attribute.

DEFAULT
Specifies that the default value should be used based on how the corresponding column is defined in the table. The value that is inserted depends on how the column was defined.
  • If the column was defined as a generated column based on an expression, the column value will be generated by the system, based on the expression.
  • If the column was defined using the IDENTITY clause, the value is generated by the database manager.
  • If the column was defined using the WITH DEFAULT clause, then the value is set to the default defined for the column (see default-clause in ALTER TABLE).
  • If the column was defined without specifying the WITH DEFAULT clause, the GENERATED clause, or the NOT NULL clause, then the value used is NULL.
  • If the column was defined using the NOT NULL clause and the GENERATED clause was not used, or the WITH DEFAULT clause was not used, or DEFAULT NULL was used, the DEFAULT keyword cannot be specified for that column (SQLSTATE 23502).

The only value that a generated column defined with the GENERATED ALWAYS clause can be set to is DEFAULT (SQLSTATE 428C9).

The DEFAULT keyword cannot be used as the value in an attribute assignment (SQLSTATE 429B9).

row-fullselect
A fullselect that returns a single row with the number of columns corresponding to the number of column-names specified for assignment. The values are assigned to each corresponding column-name. If the result of the row-fullselect is no rows, then null values are assigned.

A row-fullselect may contain references to columns of the target table of the UPDATE statement. For each row that is updated, the value of such a column in an expression is the value of the column in the row before the row is updated. An error is returned if there is more than one row in the result (SQLSTATE 21000).

WHERE
Introduces a condition that indicates what rows are updated. You can omit the clause, give a search condition, or name a cursor. If the clause is omitted, all rows of the table or view are updated.

search-condition
Is any search condition as described in Language Elements. Each column-name in the search condition, other than in a subquery, must name a column of the table or view. When the search condition includes a subquery in which the same table is the base object of both the UPDATE and the subquery, the subquery is completely evaluated before any rows are updated.

The search-condition is applied to each row of the table or view and the updated rows are those for which the result of the search-condition is true.

If the search condition contains a subquery, the subquery can be thought of as being executed each time the search condition is applied to a row, and the results used in applying the search condition. In actuality, a subquery with no correlated references is executed only once, whereas a subquery with a correlated reference may have to be executed once for each row.

CURRENT OF cursor-name
Identifies the cursor to be used in the update operation. The cursor-name must identify a declared cursor as explained in DECLARE CURSOR. The DECLARE CURSOR statement must precede the UPDATE statement in the program.

The table or view named must also be named in the FROM clause of the SELECT statement of the cursor, and the result table of the cursor must not be read-only. (For an explanation of read-only result tables, see DECLARE CURSOR.)

When the UPDATE statement is executed, the cursor must be positioned on a row; that row is updated.

This form of UPDATE cannot be used if the target of the update is a view that includes an OLAP function in the select list of the fullselect that defines the view (SQLSTATE 42828).

Rules

Notes

Examples


Footnotes:

107
The package used to process the statement is precompiled using option LANGLEVEL with value SQL92E or MIA.

108
A column of a partitioning key is not updatable (SQLSTATE 42997). The row of data must be deleted and inserted to change columns in a partitioning key.

109
There is no casting of the previous value to the source type prior to the computation.


[ Top of Page | Previous Page | Next Page ]