Sql updating multiple rows from another table
In this article I will show you the most common methods of using the UPDATE statement.
Below is the basic syntax for the UPDATE statement: This is not the complete syntax of the update statement.
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.
Below is the TSQL code to create and populate this table.
These two different tables will be used in my UDPATE statement examples below.
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.
If you incorrectly specify your search condition you might update too many rows, or not enough rows.
If you want to review the complete syntax of the UPDATE statement then please refer to Books Online.
To properly show you how to use the UPDATE statement I will need to create a few tables to hold some sample data. Below is the code to create my Toy table: In order to show you how to UPDATE a table from data in another table I need to build a second sample data table that I will call New Toy Price.
There may be times when you don’t want to manually write a bunch of UPDATE statements with different literal strings to update your table.
Suppose I wanted to change all the prices of my Toys with a single UPDATE statement.