Modifies existing rows of a single table or a view that contains only one table.
UPDATE table ... SET [column-name = expression, ... ... [ FROM table-expression, ] ... [ WHERE search-condition ] ... [ ORDER BY expression [ ASC | DESC ] ,... ]
FROM table-expression table-expression: table-spec | table-expression join-type table-spec [ ON condition ] | table-expression, ...
Transfer employee Philip Chin (employee 129) from the sales department to the marketing department.
UPDATE employee SET dept_id = 400 WHERE emp_id = 129 ;
The Marketing Department (400) will increase bonus from 4% to 6% of each employee’s base salary.
UPDATE employee SET bonus = base * 6/100 WHERE dept_id=400;
Each employee gets a pay increase with her department bonus.
UPDATE employee SET emp.salary = emp.salary + dept.bonus FROM employee emp, department dept WHERE emp.deptnum = dept.deptnum;
Here’s another way to give each employee a pay increase with the department bonus.
UPDATE employee SET emp.salary = emp.salary + dept.bonus FROM employee emp JOIN department dept ON emp.deptnum = dept.deptnum;
The UPDATE statement is used to modify rows of a single table or view that contains only one table. The table may be a base table or a temporary table. The base table cannot be part of any join index. Each named column is set to the value of the expression on the right hand side of the equal sign. Even column-name can be used in the expression—the old value will be used.
The FROM clause can contain multiple tables with join conditions and returns all the columns from all the tables specified and filtered by the join condition and/or WHERE condition.
Using the wrong join condition in a FROM clause causes unpredictable results. If the FROM clause specifies a one-to-many join and the SET clause references a cell from the “many” side of the join, the cell is updated from the first value selected. In other words, if the join condition causes multiple rows of the table to be updated per row id, the first row returned becomes the update result. For example:
UPDATE T1 SET T1.c2 = T2.c2 FROM T1 JOIN TO T2 ON T1.c1 = T2.c1
If table T2 has more than one row per T2.c1, results might be as follows:
T2.c1 T2.c2 T2.c3
1 4 3
1 8 1
1 6 4
1 5 2
With no ORDER BY clause, T1.c2 may be 4, 6, 8, or 9.
With ORDER BY T2.c3
, T1.c2 will
be updated to 8.
With ORDER BY T2.c3 DESC
, T1.c2 will
be updated to 6.
Sybase IQ rejects any UPDATE statement in which the table being updated is on the null-supplying side of an outer join. In other words:
In a left outer join, the table on the left side of the join must not be missing any rows on joined columns.
In a right outer join, the table on the right side of the join must not be missing any rows on joined columns.
In a full outer join, neither table can be missing any rows on joined columns.
For example, in the following statement, table T1 is on the left side of a left outer join, and thus cannot contain be missing any rows:
UPDATE T1 SET T1.c2 = T2.c4 FROM T1 LEFT OUTER JOIN T2 ON T1.rowid = T2.rowid
Normally, the order in which rows are updated does not matter. However, in conjunction with the NUMBER(*) function, an ordering can be useful to get increasing numbers added to the rows in some specified order. If you are not using the NUMBER(*) function, avoid using the ORDER BY clause, because the UPDATE statement performs better without it.
In an UPDATE statement, if the NUMBER(*) function is used in the SET clause and the FROM clause specifies a one-to-many join, NUMBER(*) generates unique numbers that increase, but do not increment sequentially due to row elimination. For more information about the NUMBER(*) function, see “NUMBER function [Miscellaneous]”.
You can use the ORDER BY clause to control the result from an UPDATE when the FROM clause contains multiple joined tables.
Sybase IQ ignores the ORDER BY clause in searched UPDATE and returns a message that the syntax is not ANSI valid syntax.
If no WHERE clause is specified, every row is updated. If you specify a WHERE clause, then Sybase IQ only updates rows satisfying the search condition.
The left hand side of each SET clause must be a column in a base table.
Views can be updated provided the SELECT statement defining the view does not contain a GROUP BY clause, an aggregate function, or involve a UNION operation. The view should contain only one table.
Character strings inserted into tables are always stored in the case they are entered, regardless of whether the database is case sensitive or not. Thus a character 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.
If the update violates any check constraints, the whole statement is rolled back.
Sybase IQ supports scalar subqueries within the SET clause, for example:
UPDATE r SET r.o= (SELECT MAX(t.o) FROM t ... WHERE t.y = r.y), r.s= (SELECT SUM(x.s) FROM x ... WHERE x.x = r.x) WHERE r.a = 10
See the CREATE TABLE statement for details about updating IDENTITY/AUTOINCREMENT columns.
None.
Sybase With the following exceptions, syntax of the IQ UPDATE statement is generally compatible with the Adaptive Server Enterprise UPDATE statement Syntax 1: Sybase IQ supports multiple tables with join conditions in the FROM clause.
The Transact-SQL ROWCOUNT option has no effect on UPDATE operations in Sybase IQ.
Updates of remote tables are limited to Sybase IQ syntax supported by CIS, as described in Chapter 17, “Server Classes for Remote Data Access” and Chapter 16, “Accessing Remote Data” in the Sybase IQ System Administration Guide.
Must have UPDATE permission for the columns being modified.