Entering content frame

UPDATE Statement (update_statement) Locate the document in its SAP Library structure

The UPDATE statement (update_statement) updates column values in table rows.

Syntax

<update_statement> ::=
  UPDATE [OF] <table_name> [<reference_name>] SET <set_update_clause>,... [KEY <key_spec>,...] [WHERE <search_condition>] [IGNORE TRIGGER]
| UPDATE [OF] <table_name> [<reference_name>] (<column_name>,...) VALUES (<extended_value_spec>,...) [KEY <key_spec>,...] [WHERE <search_condition>] [IGNORE TRIGGER]
| 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>

<set_update_clause> ::= <column_name> = <extended_expression>
| <column_name>,... = (<extended_expression>,...)
| (<column_name>,...) = (<extended_expression>,...)
| <column_name> = <subquery>
| (<column_name>,...) = <subquery>

Explanation

The table name must identify an existing base table, view table, or 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).

·        All target columns must identify columns in the specified table, and each target column may only be listed once.

·        The number of specified values extended_value_spec must be identical to the number of target columns. The ith value specification is assigned to the ith target column.

·        The current user must have the UPDATE privilege for each target column in the specified table.
If the table name identifies a view table, even the owner of the view table may not be able to update any column values because the view table is not updateable.

·        If the specified table is a view table, only column values from rows in the base tables on which the view table is based are updated. In this case, the target columns of the specified table correspond to columns in the base tables on which the view table is based. In the following, the term target column always refers to the corresponding column in the base table.

See also:

Data Type of the Target Column and Inserted Value

Which rows are updated

The following specifications determine which rows in the table are updated:

·        Optional sequence of key specification and optional search condition
Key specification and no search condition: A row with the specified key values already exists. The corresponding values are assigned to the target columns of this row. No rows are updated if a row with the specified key values does not exist.
Key specification and a search condition: A row containing the specified key values exists. The search condition is applied to this row. If the search condition is satisfied, the corresponding values are assigned to the target columns of this row. No rows are updated if a row with the specified key values does not exist or if a search condition applied to a row is not fulfilled.
No key specification and a search condition: The search condition is applied to each row in the specified table. The corresponding values are assigned to the target columns of all rows that satisfy the search condition.

·        If CURRENT OF is used, that is, if the cursor position in the result table result_table_name is specified: If the cursor is not positioned on a row in the result table, no rows are updated.

·        If none of the above specifications were made, all of the rows in the specified table are updated.

·        If no row is found that satisfies the conditions defined by the optional clauses, the following return code appears: 100 ROW NOT FOUND.

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, columns might exist which can be updated only in combination with other columns.

Determining the column combination for a column of a join view table

To determine the combination of columns for a given column v in a join view table, use the following procedure:

...

       1.      Determine the base table Tj containing the column which corresponds to v.

       2.      Determine the unique table sequence Ti1...Tik that contains Tj.

       3.      Determine the last table Ti1 in this sequence that has a 1:1 relationship with the key table.

       4.      The columns in the join view table that correspond to the foreign key columns of Ti1 in the referential CONSTRAINT definition between Ti1 and Ti1+1 that is relevant for the join view table are elements of the column combination.

       5.      All columns of the join view table which correspond to columns of the tables Ti1+1...Tik are elements of the column combination.

To update the column value of the relevant column, a value must be specified for each of the columns of the column combination. This is true of all target columns that fulfill the following conditions:

·        The target columns are located in a base table that is not a key table of the join view table and does not have a 1:1 relationship with the key table of the join view table.

·        The target columns are foreign key columns of a referential CONSTRAINT definition that is relevant for the join view table.

SET <set_update_clause>

The expression in a SET UPDATE clause set_update_clause must not contain a set function.

The subquery must produce a result table with at most one row. The number of columns must be equal to the number of target columns specified.

IGNORE TRIGGER

If you do not want an UPDATE trigger to be executed for the specified table, you can specify IGNORE TRIGGER. This prevents endless recursion if updated rows are updated again in the trigger.

CURRENT OF

If CURRENT OF is specified, the table name in the FROM clause of the QUERY statement, with which the result table result_table_name 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, the system checks whether each row that was updated fulfills 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 in the corresponding referenced table 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 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.

 

Leaving content frame