Update a table from a Select query

I had to update a table with missing product id’s using the item master table to do the correlation. This led to having to formulate a lovely SQL query that would let me update the table based on matching criteria. In effect, I knew the SKU in both records should match, so I could correlate them via that data point. This SQL can easily be adapted to work with your scenario. I used this against a SQL Server 2005 database.

update torderdetails set torderdetails.productid = tproducts.productid
from torderdetails join tproducts on tproducts.partnumber = torderdetails.partnumber where torderdetails.productid =0

Hopefully this helps someone out their with the headaches of these rare tasks.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.