The UPDATE statement updates existing rows in an existing table or updateable view. Let’s examine the syntax of the UPDATE statement. The elements shown in Example 1 appear most commonly in the UPDATE statement.
Example 1. UPDATE Statement—Core Elements
UPDATE [ TOP (n) [PERCENT] ] table_or_view SET { column_to_update = { new_value | DEFAULT | NULL } | column_to_update { .WRITE (new_value, @Offset, @Length) } | @variable = new_value | @variable = column_to_update = new_value | column_to_update { += | -= | *= | /= | %= | &= | ^= | |= } new_value | @variable { += | -= | *= | /= | %= | &= | ^= | |= } new_value | @variable = column_to_update { += | -= | *= | /= | %= | &= | ^= | |= } new_ value } [FROM ... WHERE ... ]
|
The table_or_view is the name of the table or view in which you are updating rows. If you are using a view for your UPDATE
statement, this view must be updateable. Updateable views are based
upon a single source table. You could also substitute a table-valued
variable as the table_or_view.
The TOP (n) [PERCENT] clause serves the same purpose as it does in the INSERT statement. Only the top n, or top n percent of rows will be updated, if this clause is specified.
The SET keyword specifies the list of columns or variables that will be updated. The column_to_update is the name of the column containing the data that should be changed. Column_to_update must be a column from the table_or_view. Identity columns cannot be updated.
The new_value is a literal value, a variable, a subselect statement (enclosed within parentheses), or any scalar expression. When the UPDATE statement completes, the column_to_update or @variable value will be set to the new_value.
The FROM
is essentially the filter criteria for defining what data should be
updated, and what it should be updated to. If this part of the UPDATE
statement is omitted, all data in the table or view will be updated.
Ensure that you specify an alias if the table or view you are trying to
update is mentioned more than once in the FROM clause.
The WHERE clause specifies an additional filter for data that should be updated.
The column can be set to its
new value using not just the familiar equals to (=) operator, but also a
variety of other assignment operators. Table 1 lists assignment operators available. Use these operators with the SET clause of the UPDATE statement.
Table 1. Assignment Operators
Operator Sign | Use |
---|
= | Assign |
+= | Add and assign |
−= | Subtract and assign |
*= | Multiply and assign |
/= | Divide and assign |
%= | Modulo and assign |
&= | Bitwise AND and assign |
^= | Bitwise XOR and assign |
|= | Bitwise OR and assign |
UPDATE Statement Examples
Examples 2 through 4 demonstrate the most common uses of the UPDATE
statement. Feel free to run these examples against the AdventureWorks
database. Performing the examples yourself will give you a better
understanding of how the UPDATE statement is used.
Example 2. Using the UPDATE...SET...WHERE Statement
SELECT DISTINCT Color from Production.Product GO
--Change all product with NULL color to show 'N/A' USE AdventureWorks GO
UPDATE Production.Product SET Color = 'N/A' WHERE Color is NULL GO SELECT DISTINCT Color from Production.Product GO
|
Example 3. Using the UPDATE...SET...FROM...WHERE Statement with Ambiguous Table Names
-- List all employees managed by David Bradley, along with their vacation hours SELECT Employee.VacationHours, Person.Contact.FirstName + ' ' + Person. Contact.LastName as EmployeeName, ManagerContact.FirstName + ' ' + ManagerContact.LastName as ManagerName FROM HumanResources.Employee join Person.Contact ON HumanResources.Employee.ContactID = Person.Contact. ContactID join HumanResources.Employee as Manager ON HumanResources.Employee.ManagerID = Manager.EmployeeID join Person.Contact as ManagerContact ON Manager.ContactID = ManagerContact. ContactID WHERE ManagerContact.FirstName = 'David' AND ManagerContact.LastName = 'Bradley' GO
-- Add 10 vacation hours to all employees managed by David Bradley UPDATE Humanresources.Employee SET VacationHours += 10 FROM HumanResources.Employee join Person.Contact ON HumanResources.Employee.ContactID = Person.Contact. ContactID join HumanResources.Employee as Manager ON HumanResources.Employee.ManagerID = Manager.EmployeeID join Person.Contact as ManagerContact ON Manager.ContactID = ManagerContact. ContactID WHERE ManagerContact.FirstName = 'David' AND ManagerContact.LastName = 'Bradley' GO
-- List all employees managed by David Bradley, along with their vacation hours -- Ensure that the hours have been updated by the previous statement SELECT Employee.VacationHours, Person.Contact.FirstName + ' ' + Person. Contact.LastName as EmployeeName, ManagerContact.FirstName + ' ' + ManagerContact.LastName as ManagerName FROM HumanResources.Employee join Person.Contact ON HumanResources.Employee.ContactID = Person.Contact. ContactID join HumanResources.Employee as Manager ON HumanResources.Employee.ManagerID = Manager.EmployeeID join Person.Contact as ManagerContact ON Manager.ContactID = ManagerContact. ContactID WHERE ManagerContact.FirstName = 'David' AND ManagerContact.LastName = 'Bradley' GO
|
Example 4. Using the UPDATE...SET...FROM...WHERE Statement with Ambiguous Table Names
-- Show all products by cost, only from subcategies that contains 'Bikes' -- Show the ListPrice for each Product SELECT Product.Name, Product.StandardCost, Product.ListPrice, ProductSubcategory.Name FROM Production.Product JOIN Production.ProductSubcategory ON ProductSubcategory.ProductSubcategoryID = Product.ProductSubcategoryID WHERE ProductSubcategory.Name like '%Bikes%' ORDER BY Product.StandardCost DESC GO
-- Increase the ListPrice by 20% for the 10 highest-cost products, from subcategies that contains 'Bikes' UPDATE Production.Product SET Product.ListPrice *= 1.15 WHERE Product.ProductID in (SELECT TOP (10) ProductID FROM Production.Product JOIN Production.ProductSubcategory ON ProductSubcategory.ProductSubcategoryID = Product.ProductSubcategoryID WHERE ProductSubcategory.Name like '%Bikes%'
ORDER BY Product.StandardCost DESC) GO
-- Show the ListPrice for each Product after the Increase SELECT Product.Name, Product.StandardCost, Product.ListPrice, ProductSubcategory.Name FROM Production.Product JOIN Production.ProductSubcategory ON ProductSubcategory.ProductSubcategoryID = Product.ProductSubcategoryID WHERE ProductSubcategory.Name like '%Bikes%' ORDER BY Product.StandardCost DESC GO
|