ASA SQL Reference
SQL Statements
Use this statement to modify existing rows in database tables.
UPDATE [ FIRST | TOP n ] table-list SET set-item, ...
[ FROM table-list ]
[ WHERE search-condition ]
[ ORDER BY expression [ ASC | DESC ], ... ]
UPDATE table-list
SET set-item, ...
[ VERIFY ( column-name, ... ) VALUES ( expression, ... ) ]
[ WHERE search-condition ]
[ ORDER BY expression [ ASC | DESC ], ... ]
UPDATE table
PUBLICATION publication
{ SUBSCRIBE BY expression
| OLD SUBSCRIBE BY expression NEW SUBSCRIBE BY expression
}
WHERE search-condition
set-item :
column-name [.field-name...] = expression
| column-name[.field-name...].method-name( [ expression ] )
| @variable-name = expression
UPDATE clause The table is either a base table, a temporary table, or a view. Views can be updated unless the SELECT statement defining the view contains a GROUP BY clause or aggregate function, or involves a UNION operation.
FIRST or TOP clause Primarily for use with the ORDER BY clause, this clause allows you to update only a certain subset of the rows that satisfy the WHERE clause. You cannot use a variable as input with FIRST or TOP.
SET clause If you are updating Java columns, you can use field-name to update the value of a public field in the column. Alternatively, you can use a method to set the value. The following clause updates name field of the JProd column using a method:
SET JProd.setName( 'Tank Top' )
If you are updating non-Java columns, the SET clause is of the following form:
SET column-name = expression, ...
and/or
SET @variable-name = expression, ...
Each named column is set to the value of the expression on the right hand side of the equal sign. There are no restrictions on the expression. If the expression is a column-name, the old value is used. When assigning a variable, the variable must already be declared, and its name must begin with the "at" sign (@). Variable and column assignments can be mixed together, and any number can be used. If a name on the left side of an assignment in the SET list matches a column in the updated table as well as the variable name, the statement will update the column.
Following is an example of part of an UPDATE statement. It assigns a variable in addition to updating the table:
UPDATE T SET @var = expression1, col1 = expression2 WHERE...
This is equivalent to:
SELECT @var = expression1 FROM T WHERE... ; UPDATE T SET col1 = expression2 WHERE...
FROM clause The optional FROM clause allows tables to be updated based on joins. If the FROM clause is present, the WHERE clause qualifies the rows of the FROM clause. Data is updated only in the table list of the UPDATE clause.
If a FROM clause is used, it is important to qualify the table name the same way in both parts of the statement. If a correlation name is used in one place, the same correlation name must be used elsewhere. Otherwise, an error is generated.
This clause is allowed only if ANSI_UPDATE_CONSTRAINTS is set to OFF. See ANSI_UPDATE_CONSTRAINTS option [compatibility].
For a full description of joins, see Joins: Retrieving Data from Several Tables.
For more information, see FROM clause.
WHERE clause If a WHERE clause is specified, only rows satisfying the search condition are updated. If no WHERE clause is specified, every row is updated.
ORDER BY clause Normally, the order in which rows are updated does not matter. However, in conjunction with the FIRST or TOP clause the order can be significant.
You must not update columns that appear in the ORDER BY clause unless you set the ANSI_UPDATE_CONSTRAINTS option to OFF. See ANSI_UPDATE_CONSTRAINTS option [compatibility].
Case sensitivity Character strings inserted into tables are always stored in the same case as they are entered, regardless of whether the database is case sensitive or not. A CHAR data type column updated with a string Value is always held in the database with an upper case V and the remainder of the letters lower case. SELECT statements return the string as Value. If the database is not case sensitive, however, all comparisons make Value the same as value, VALUE, and so on. Further, if a single-column primary key already contains an entry Value, an INSERT of value is rejected, as it would make the primary key not unique.
Updates that leave a row unchanged If the new value does not differ from the old value, no change is made to the data. However, BEFORE UPDATE triggers fire any time an UPDATE occurs on a row, whether or not the new value differs from the old value. AFTER UPDATE triggers fire only if the new value is different from the old value.
Syntax 1 of the UPDATE statement modifies values in rows of one or more tables. Syntax 2 and 3 are applicable only to SQL Remote.
Syntax 2 is intended for use with SQL Remote only, in single-row updates executed by the Message Agent. The VERIFY clause contains a set of values that are expected to be present in the row being updated. If the values do not match, any RESOLVE UPDATE triggers are fired before the UPDATE proceeds. The UPDATE does not fail simply because the VERIFY clause fails to match.
Syntax 3 of the UPDATE statement is used to implement a specific SQL Remote feature, and is to be used inside a BEFORE trigger.
It provides a full list of SUBSCRIBE BY values any time the list changes. It is placed in SQL Remote triggers so that the database server can compute the current list of SUBSCRIBE BY values. Both lists are placed in the transaction log.
The Message Agent uses the two lists to make sure that the row moves to any remote database that did not have the row and now needs it. The Message Agent also removes the row from any remote database that has the row and no longer needs it. A remote database that has the row and still needs it is not be affected by the UPDATE statement.
For publications created using a subquery in a SUBSCRIBE BY clause, you must write a trigger containing syntax 3 of the UPDATE statement in order to ensure that the rows are kept in their proper subscriptions.
Syntax 3 of the UPDATE statement allows the old SUBSCRIBE BY list and the new SUBSCRIBE BY list to be explicitly specified, which can make SQL Remote triggers more efficient. In the absence of these lists, the database server computes the old SUBSCRIBE BY list from the publication definition. Since the new SUBSCRIBE BY list is commonly only slightly different from the old SUBSCRIBE BY list, the work to compute the old list may be done twice. By specifying both the old and new lists, you can avoid this extra work.
The SUBSCRIBE BY expression is either a value or a subquery.
Syntax 3 of the UPDATE statement makes an entry in the transaction log, but does not change the database table.
Updating a significant amount of data using the UPDATE statement will also update column statistics.
Must have UPDATE permission for the columns being modified.
None.
Joins: Retrieving Data from Several Tables
SQL/92 Syntax 1 is an entry-level feature, except for the FROM and ORDER BY clauses, which are vendor extensions. Syntax 2 and 3 are vendor extensions for use only with SQL Remote.
SQL/99 Syntax 1 is a core feature, except for the FROM and ORDER BY clauses, which are vendor extensions. Syntax 2 and 3 are vendor extensions for use only with SQL Remote.
To enforce SQL/92 compatibility, ensure that the ANSI_UPDATE_CONSTRAINTS option is set to STRICT.
For more information, see ANSI_UPDATE_CONSTRAINTS option [compatibility].
Sybase Subject to the expressions being compatible, the syntax of the UPDATE statement (syntax 1) is compatible between Adaptive Server Enterprise and Adaptive Server Anywhere. Syntax 2 and 3 are not supported.
Transfer employee Philip Chin (employee 129) from the sales department to the marketing department.
UPDATE employee SET dept_id = 400 WHERE emp_id = 129;
Sales orders currently start at ID 2001. Renumber all existing sales orders by subtracting 2000 from the ID.
UPDATE sales_order AS orders SET orders.id = orders.id - 2000 ORDER BY items.id ASC
This update is possible only if the foreign key of the sales_order_items table (referencing the primary key sales_order.id) is defined with the action ON UPDATE CASCADE. The sales_order_items table is then updated as well.
For more information on foreign key properties, see ALTER TABLE statement and CREATE TABLE statement.