Tag Archives: sql

T-SQL Update Table From Another Table

Let say you have two tables and you want to update values in first table with values from second table. That could be for example if you have Product table and you have to load new prices and discounts from Import_Prices table.

The T-SQL code to update data could look like this:

UPDATE Product
SET Product.Price = Import_Prices.Price,
    Product.Discount = Import_Prices.Discount
FROM Product INNER JOIN Import_Prices
   ON Product.ID = Import_Prices.ProductID

In this case column ID of table Product corresponds to column ProductID of table Import_Prices .

Be careful to define relationship correctly and eventually filter only needed rows with WHERE clause. UPDATE command could change all table rows if you don’t narrow it correctly, so be sure that you have backup created before using ad-hoc UPDATE query :)