Changes data in existing rows, either by adding new data or by modifying existing data. You must use aliases to distinguish between updates of tables and columns with the same names.
For additional syntax information for the UPDATE statement, see the Microsoft SQL Server Transact-SQL Reference.
UPDATE {table_name | view_name}
SET
[column_name = {column_list | variable_list | variable_and_column_list}
[, column_name2 = {column_list2 | variable_list2 |
variable_and_column_list2}
...[, column_nameN = {column_listN | variable_listN
| variable_and_column_listN}]]
| expression]
[FROM {table_name1 | view_name1}[(optimizer_hints)]
[, {table_name2 | view_name2}[(optimizer_hints)]
[..., {table_name16 | view_name16}[(optimizer_hints)]]]]
where
Each table_name or view_name can be given an alias, either for convenience or to distinguish a table or view in a self-join or subquery. Aliases (when defined) must be used for ambiguous column references and must match the alias reference (the full table name cannot be used if an alias has been defined). To use an alias, specify the object name, a space, and then the alias name as follows:
UPDATE titles SET t.ytd_sales = t.ytd_sales + qty FROM titles t, sales s WHERE t.title_id = s.title_id
The order of the tables and views after the FROM keyword does not affect the results set returned.
An optimizer_hint can be one or more of the following.
Option | Description |
---|---|
TABLOCK | Performs an exclusive lock on the table that is held until the end of command. TABLOCKX can be substituted for TABLOCK. |
PAGLOCK | Performs exclusive page locks where a single exclusive table lock would normally be taken. |
TABLOCKX | Performs an exclusive lock on the table that is held until the end of command. TABLOCK may be substituted for TABLOCKX. |
Important Some of the optimizer hints can be used in combination. (For example, TABLOCK and PAGLOCK). When multiple options are specified, separate them with a space; the more restrictive option will take precedence.
The update table must be unambiguous for updates to occur. If two tables in a query have the same column name, use aliases to distinguish them.
If aliases are used, the UPDATE statement will cause an error when the table that should be updated is ambiguous. Trace flag 110 will disable these changes.
For more information about trace flag 110, see Trace Flags.
This example references the same table twice in the WHERE clause or different tables with the same name. In earlier versions, changes were made to the first occurrence of the table. In version 6.5 an error message is returned.
Table stockref
id |
qty |
type |
1 |
10 |
'a' |
2 |
10 |
'a' |
3 |
10 |
'a' |
4 |
10 |
'a' |
1 |
30 |
'b' |
2 |
40 |
'b' |
3 |
50 |
'b' |
4 |
60 |
'b' |
update stockref set s2.qty = s1.qty from stockref s1, stockref s2 where s1.id = s2.id and s1.type = 'a' and s2.type = 'b'
This SELECT statement divides the stockref table into two parts, the 'a' range and the 'b' range, matches them on the id field and then copies the qty value from the 'b' range and replaces the qty values in the 'a' range. The UPDATE stockref section of the syntax is ambiguous because it does not specify whether to update the 'a' range or the 'b' range.
The SET s2.qty = s1.qty section of syntax specifies which range to update. In order to avoid an error, use an alias on the ambiguous table reference to indicate which range of the stockref table to update.
UPDATE s2 SET s2.qty = s1.qty FROM stockref s1, stockref s2 WHERE s1.id = s2.id AND s1.type = 'a' AND s2.type = 'b
This example uses the PAGLOCK optimizer_hint to use a shared page lock when updating the author's last name from Yokohama to Yokomoto in the authors table.
UPDATE authors SET au_lname = 'Yokohama' FROM authors(PAGLOCK) WHERE au_lame = 'Yokomoto'