Richard C.
—In Microsoft SQL Server, you might know how to update a table like this:
UPDATE Product SET Price = 600 WHERE Id = 3;
But how do you update a table with values from other tables? In other words, how do you use a SELECT statement in an UPDATE statement?
Let’s create an example database to demonstrate the problem. We have a Product
table, where each product we sell has a code and price in dollars:
CREATE TABLE Product ( Id INT PRIMARY KEY, Name NVARCHAR(255), Code NVARCHAR(4), Price FLOAT ); INSERT INTO Product(Id, Name, Code, Price) VALUES (1, 'Table', 'TABL', 100), (2, 'Chair', 'CHAR', 50), (3, 'Desk', 'DESK', 150);
We want to update our Product
table with new prices from the PriceUpdate
table below:
CREATE TABLE PriceUpdate ( Id INT PRIMARY KEY, Code NVARCHAR(4), Price FLOAT ); INSERT INTO PriceUpdate(Id, Code, Price) VALUES (89, 'TABL', 110), (98, 'CHAR', 65), (45, 'DESK', 155);
You probably learned how to use joins when learning to write SELECT statements in SQL. You can use them in an UPDATE statement too. In the following code, we update our Product
table with the new prices by matching against the PriceUpdate
table on the shared Code
column:
UPDATE P SET P.Price = U.Price FROM Product P JOIN PriceUpdate U ON P.Code = U.Code;
Note that you have to give both tables an alias (Product P
) so that SET knows which table you want to update. If your tables have thousands of rows, you’ll also want to add an index to both Code
columns so that your query will run quickly.
Alternatively, you could update using a subquery:
UPDATE Product SET Price = (SELECT Price FROM PriceUpdate WHERE Code = Product.Code);
In general, you should use joins instead of subqueries. Joins are usually faster, although speed can vary depending on the exact tables and query you are using. If in doubt, always check the query execution plan in SQL Server.
Finally, if you want to do a complex query, combining UPDATE, INSERT, and DELETE, you can use the MERGE statement. It’s been available since SQL Server 2008.
MERGE INTO Product P USING PriceUpdate U ON P.Code = U.Code WHEN MATCHED THEN UPDATE SET P.Price = U.Price WHEN NOT MATCHED THEN INSERT (Code, Price) VALUES (U.Code, U.Price);
In this example, the WHEN NOT MATCHED clause isn’t used and can be removed, but you can see how it might be useful. This solution is the most specific to SQL Server, so you’ll want to avoid using it if you need to run your queries on MySQL or PostgreSQL in the future.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at [email protected].
If you are a California resident, see our Supplemental notice.