Posted by: Denny Cherry
Back To Basics, SQL, SQL Server 2000, SQL Server 2005, SQL Server 2008, T/SQL, UPDATE
After you’ve inserted the data into the table, it’s time to update the data. We do this by using the UPDATE statement. The update statment can be used in two ways. The first is to update a record or set of records in a single table, by simply filtering the data in the table by using values in the table.
SET Column1 = 'Value'
WHERE AnotherColumn = 'AnotherValue'
A more complex update uses another table as the source of the data. This makes the UPDATE statement look like a combination of the UPDATE statement and the SELECT statement.
SET Column2 = AnotherTable.Column3
WHERE TableName.Column1 = AnotherTable.Column1
We can add joins into this as well, so that we can update more than one column from different tables at the same time.
SET Column2 = AnotherTable.Column3,
Column3 = ThirdTable.Column2
JOIN ThirdTable ON AnotherTable.Column5 = ThirdTable.Column4
WHERE TableName.Column1 = ThirdTable.Column1
I hope that you find this post useful. I encourage everyone to open up Books OnLine and read through the information on the UPDATE statement. It includes more examples, and some of the other options which are available to you.