Updating one table from another table

By doing this I can see what my WHERE condition will return to make sure it identifies the same rows I want to update.

Once my SELECT statement does return the correct rows I can then copy the WHERE clause from my SELECT statement, and paste it into my UDPATE statement code.

I can do that with the following code: In this code I updated the Toy table based on values in another table, in this case a table named New Toy Price.

To accomplish that I joined the TOY table to my New Toy Price table based on the ID column.

Below is an example that does this: I updated the Toy Name and the Price for the Toy row with an ID value of 4, which was my original Super Surfer row.

I did this by having a single SET clause in my UPDATE statement, with the two column name/value pairs separated with a comma.

In this article I will show you a number of different ways to use the UPDATE statement to modify the data in your SQL Server tables.

If you incorrectly specify your search condition you might update too many rows, or not enough rows.

If you are not exactly sure what rows will be returned by your WHERE clause of an UPDATE statement, it best to take some precautions prior to executing your UPDATE statement.

What I like to do is to first execute a SELECT statement that contains the I plan to use in my WHERE clause of my UPDATE statement.

Suppose I wanted to change both the Toy Name and the Price columns.

I can update multiple columns with a single UPDATE statement.

717

Leave a Reply