The UPDATE statement changes column values in table rows.
Syntax
<update_statement> ::=
UPDATE [OF] <table_name> [<reference_name>] SET <set_update_clause>,... [KEY <key_spec>,...] [WHERE <search_condition>]
| UPDATE [OF] <table_name> [<reference_name>] (<column_name>,...) VALUES (<extended_value_spec>,...) [KEY <key_spec>,...] [WHERE <search_condition>]
| UPDATE [OF] <table_name> [<reference_name>] SET <set_update_clause>,... WHERE CURRENT OF <result_table_name>
| UPDATE [OF] <table_name> [<reference_name>] (<column_name>,...) VALUES (<extended_value_spec>,...) WHERE CURRENT OF <result_table_name>
Explanation
The table name must denote an existing base table, view table (see
Table), or a synonym.Columns whose values are to be updated are called target columns. One or more target columns and new values for these columns are specified after the table name and reference name (if necessary).
The following specifications determine the rows in the table that are updated:
Even values in key columns that were defined by a user in a
CREATE TABLE statement or ALTER TABLE statement can be updated. The implicit key column SYSKEY, if created, cannot be updated.If the table name specifies a join view table (see
CREATE VIEW statement), columns may exist that can only be updated in conjunction with other columns (determine the column combination for a column or a join view table). To update the value of the relevant column, a value must be specified for all of the columns in the column combination. This is true of all target columns that fulfill the following conditions:CURRENT OF
If CURRENT OF is specified, the table name in the
FROM clause of the QUERY statement, with which the result table was built, must be identical to the table name in the UPDATE statement.If CURRENT OF is specified and the cursor is positioned on a row in the result table, the corresponding values are assigned to the target columns of the corresponding row. The corresponding row is the row of the table specified in the FROM clause of the query statement, from which the result table row was formed. This procedure requires that the result table was specified with FOR UPDATE. It is impossible to predict whether or not the updated values in the corresponding row are visible the next time the same row of the result table is accessed.
Reasons for an UPDATE statement failure
If
CONSTRAINT definitions exist for base tables in which rows were updated with the UPDATE statement, each row that was updated is checked against the CONSTRAINT definitions. The UPDATE statement fails if this is not the case for at least one modified row.For each row in which the value of foreign key columns has been updated with the UPDATE statement, the database system checks whether the respective resulting foreign key exists as a key or as a value of an index defined with UNIQUE (see
CREATE INDEX statement) in the corresponding referenced table. The UPDATE statement fails if this is the case for at least one modified row.For each row in which the value of a referenced column of a
referential CONSTRAINT definition is to be updated using the update statement, the database system checks whether there are rows in the corresponding foreign key table that contain the old column values as foreign keys. The UPDATE statement fails if this is the case for at least one row.If
triggers that are to be executed after an UPDATE statement were defined for base tables in which rows are to be updated with the UPDATE statement, they are executed accordingly. The UPDATE statement will fail if one of these triggers fails.Further information
The update statement can only be used to assign a value to columns with the data type LONG if it contains a parameter or NULL specification. The assignment of values to LONG columns is therefore only possible with some database tools.
In the case of the UPDATE statement, the third entry of SQLERRD in the SQLCA is set to the number of updated rows. Rows are also counted as updated when the old value was overwritten with a new but identical value.
If errors occur while rows are updated, the UPDATE statement fails, leaving the table unchanged.