W
| Lesson 3 | Overview of updating data |
| Objective | Describe how to update data with Transact-SQL. |
UPDATE Transact-SQL statement, specifying the names of the specific columns you want to change. The UPDATE statement uses the following general syntax:
UPDATE table_name
SET column_name1 = value1, column_name2 = value2, ...
WHERE condition;
table_name is the table containing the data you want to update.column_name1, column_name2, ... are the columns you want to update.value1, value2, ... are the new values for those columns.condition identifies which rows should be updated; only rows that satisfy it are changed.Employees with columns EmployeeID, FirstName, LastName, and Salary. To update the salary of the employee with EmployeeID 101:
UPDATE Employees
SET Salary = 60000
WHERE EmployeeID = 101;
WHERE clause, every row in the target table is eligible for the update, so always include a precise condition unless you genuinely intend to change every row. SET can also use expressions rather than only literal values, which is how you increment or adjust an existing value in place:
UPDATE table_name
SET column_name = column_name + 1
WHERE condition;
UPDATE can pull its new values from another table, combined with FROM and a JOIN:
UPDATE p
SET Price = s.NewPrice
OUTPUT inserted.ProductID,
deleted.Price AS OldPrice,
inserted.Price AS NewPrice
FROM dbo.Product AS p
JOIN dbo.PriceChange AS s
ON s.ProductID = p.ProductID
WHERE s.Approved = 1;
p is the target table's alias, and the join against dbo.PriceChange supplies the new price for each matching row. The OUTPUT clause here returns both the old and new values in one step: deleted represents each row's values before the update, and inserted represents its values after.
OUTPUT values are produced before any AFTER triggers run; if an AFTER trigger later causes the statement or transaction to fail, the client may already have received OUTPUT results for a change that didn't ultimately persist. Treat OUTPUT results as provisional until you know the statement actually succeeded.
OPENQUERY/OPENROWSET against a remote data source, provided the underlying provider supports remote updates.WITH (ROWLOCK) follows the target table; a query hint such as OPTION (RECOMPILE) goes at the very end of the statement. Both override SQL Server's own choices for locking or plan compilation, so use them only after you've actually measured a problem, not as a default habit.WHERE CURRENT OF cursor_name updates the single row the cursor is currently positioned on, and requires an open, fetched, updatable cursor.
SET @OldPrice = Price,
Price = Price + 5.00
while a chained assignment captures the post-update value instead:
SET @NewPrice = Price = Price + 5.00
Both of these only make sense when the WHERE clause restricts the statement to a single row; a local variable can only hold one value, so if multiple rows are affected, which row's value ends up in the variable is undefined. For multiple rows, use OUTPUT instead, as shown above, to return old and new values for every row that changed.INSERT, UPDATE, DELETE, and MERGE statements. It does not change UPDATE syntax at all, only how SQL Server manages locks internally while the statement runs.
INSERT statement, which can supply explicit values for an IDENTITY column once IDENTITY_INSERT is turned on, an UPDATE statement cannot change the value of an IDENTITY column at all; there is no equivalent override.
DELETE statement.