T-SQL » Statements » UPDATE

Syntax:
UPDATE table SET col_name1 = newvalue1 [ , col_name2 = newvalue2, ... ] [ WHERE criteria ]
table
Specifies the table within a database that is to be updated.
col_name1
The column(s) that is to be updated.
newvalue1
The value(s) that is to be updated in the column.
criteria
The condition(s) that determine which of the rows are to be updated.

The UPDATE statement is used to change values in one, or more, or all of the records in a table.

Examples

Code:
UPDATE Project
SET ProjectName = 'Grindstone'
WHERE ProjectName = 'Hardwork';
Output:
(5 row(s) affected)
Explanation:

The UPDATE statement can be used in updating a single field in a table.

Note: The UPDATE doesn't generate a result set. If you want to know which records will be modified, first run a SELECT query that uses the same criteria. If the results are satisfactory, then run the update query.

Language(s): MS SQL Server
Code:
UPDATE Products
SET ShippingWeight = '800lbs', ProductName = 'Grindstone MarkII', Price = '$348.71'
WHERE CatalogNumber = 'GS1097';
Output:
(1 row(s) affected)
Explanation:

The UPDATE statement can also be used to update more than one field in a table.

Language(s): MS SQL Server
Code:
UPDATE MusicArtists
SET Instrument = 'violin'
WHERE Instrument = 'fiddle'
AND Style = 'classical';
Output:
(1 row(s) affected)
Explanation:

This example updates the 'MusicArtists' table by replacing any occurrence of the instrument 'fiddle' with 'violin' if the music type is 'classical'.

Language(s): MS SQL Server
Code:
UPDATE TrainFares
SET Fare = Fare * 1.1
WHERE Line = 'Edinburgh'
AND Journey = 'single';
Output:
(4 row(s) affected)
Explanation:

This example updates the 'TrainFares' table to account for a 10% increase in the cost of a single ticket on the Edinburgh line.

Language(s): MS SQL Server

See Also: