For SQL Server 6.5 information, see UPDATE Statement in What's New for SQL Server 6.5.
Changes data in existing rows, either by adding new data or by modifying existing data.
UPDATE {table_name | view_name}
SET [{table_name | view_name}]
{column_list
| variable_list
| variable_and_column_list}
[, {column_list2
| variable_list2
| variable_and_column_list2}
... [, {column_listN
| variable_listN
| variable_and_column_listN}]]
[WHERE clause]
where
Specifies the name of the table or view used in the UPDATE statement. If the table or view is not in the current database, use a fully qualified table_name or view_name (database_name.owner.object_name).
DEFAULT is not valid for an identity column; columns with the IDENTITY property should not be explicitly updated. For more information, see the CREATE TABLE statement.
Sets local variables as well as columns. This syntax provides the same functionality as an UPDATE statement followed by a SELECT statement in an explicitly defined transaction. Because the combination of selecting and updating is performed during a single UPDATE statement, the transaction is implicit and locks are held for a shorter period of time. For sample syntax, see "Setting Variables and Columns," later in this topic.
Is used to perform a searched update (using search_conditions) or a positioned update (using CURRENT OF cursor_name). When no WHERE clause is given in the UPDATE statement, all rows in the table are modified.
The IDENTITYCOL keyword can be used in the place of a column_name that has the IDENTITY property. For more information, see the CREATE TABLE statement.
Use the UPDATE statement to change single rows, groups of rows, or all rows in a table. UPDATE specifies which row(s) to change and provides the new data. When updating rows, these rules apply:
All trailing spaces are removed from data inserted into varchar columns, except in strings that contain only spaces. These strings are truncated to a single space.
Constraints are defined with the CREATE TABLE or ALTER TABLE statement. Rules are created with the CREATE RULE statement and bound with the sp_bindrule system stored procedure.
Note The UPDATE statement is logged; if you are replacing or modifying large blocks of text or image data, use the WRITETEXT or UPDATETEXT statement instead of the UPDATE statement. The WRITETEXT and UPDATETEXT statements (by default) are not logged. For details, see the Text and Image Manipulation topic.
In SQL Server 6.0, various techniques are used to determine whether or not an UPDATE can be performed "in-place." When an "update-in-place" occurs, the overall performance of the UPDATE statement is improved because the update is direct (only the row modifications to the page are logged). The decision as to whether or not an update-in-place can occur is based on the following requirements. If these requirements are not true, a deferred update will take place. A deferred update is a delete followed by an insert.
To see the update strategy used (DIRECT or DEFERRED), set the SHOWPLAN session setting. For details, see the SET statement.
In addition to the syntax shown earlier, Transact-SQL includes a feature that allows you to select data from a table or tables and update corresponding data in the first named table. The functionality provided is similar to that of using a correlated subquery as a search_condition in the WHERE clause of a standard UPDATE.
Transact-SQL extension syntax:
UPDATE {table_name | view_name}
SET [{table_name | view_name}]
{column_list
| variable_list
| variable_and_column_list}
[, {column_list2
| variable_list2
| variable_and_column_list2}
... [, {column_listN
| variable_listN
| variable_and_column_listN}]]
[FROM {table_name | view_name}
[, {table_name | view_name}]...]
[..., {table_name16 | view_name16}]]
[WHERE clause]
where
For an example of the differences between correlated subqueries and this Transact-SQL extension, see the examples later in this section.
In earlier releases of SQL Server, it was impossible to verify both the new and old values of a modified column. Values returned from the SELECT statement could not be guaranteed at the time of the UPDATE unless an explicitly defined transaction using HOLDLOCK was defined.
In earlier releases:
BEGIN TRANSACTION
SELECT variable_name = column_name1
FROM table_name
WHERE column_name2 = expression
HOLDLOCK
UPDATE table_name
SET column_name1 = expression
WHERE column_name2 = expression
COMMIT TRANSACTION
In SQL Server 6.0:
UPDATE table_name
SET column_name1 = expression, variable_name = column_name1
WHERE column_name2 = expression
In this single update (an implicit transaction), the locks are held only until the update completes (no need to hold them for both an UPDATE and a SELECT).
UPDATE permission defaults to the table owner, who can transfer it to other users.
These examples show how all rows can be affected if a WHERE clause is eliminated from an UPDATE statement.
In this example, if all the publishing houses in the publishers table move their head offices to Atlanta, Georgia, this is how the publishers table could be updated:
UPDATE publishers SET city = 'Atlanta', state = 'GA'
This example changes the names of all the publishers to NULL:
UPDATE publishers SET pub_name = NULL
You can also use computed column values in an update. This example doubles all prices in the titles table:
UPDATE titles SET price = price * 2
The WHERE clause specifies which rows are to be updated. For example, in the unlikely event that northern California is renamed Pacifica (abbreviated PC) and the people of Oakland vote to change the name of their city to Bay City, here is how to update the authors table for all former Oakland residents whose addresses are now out of date:
UPDATE authors SET state = 'PC', city = 'Bay City' WHERE state = 'CA' AND city = 'Oakland'
You must write another statement to change the name of the state for residents of other northern California cities.
To modify the ytd_sales column to reflect the most recent sales recorded in the sales table, this example assumes that only one set of sales is recorded for a given title on a given date and that updates are current.
UPDATE titles SET ytd_sales = ytd_sales qty FROM titles, sales WHERE titles.title_id = sales.title_id AND sales.date = (SELECT MAX(sales.date) FROM sales)
This example assumes that only one set of sales is recorded for a given title on a given date and that updates are current. If this is not the case (if more than one sale for a given title can be recorded on the same day), then the example shown here does not work correctly. It executes without error, but each title is updated with only one sale, regardless of how many sales actually occurred on that day. This is because a single UPDATE statement never updates the same row twice.
In the situation where more than one sale for a given title can occur on the same day, all the sales for each title must be aggregated together within the UPDATE statement, as shown in this example:
UPDATE titles SET ytd_sales = (select sum(qty) FROM sales WHERE sales.title_id = titles.title_id AND sales.date IN (SELECT MAX(date) FROM sales)) FROM titles, sales
CREATE INDEX | DELETE |
CREATE TABLE | INSERT |
CREATE TRIGGER | Text and Image Manipulation |
Cursors | UPDATE |