In my scenario I have a table where column names are stored in rows. Using this table columns and data I have to update another table column values. Below are my tables:
Table:Product
| ProductName | ProductCount | InventoryId |
| ------------ | ------------ | ------------ |
| Bicycles | 100 | 101 |
| Baby Products | 200 | 101 |
| Books | 30 | 101 |
Table: Inventory
InventoryId | Bicycles | BabyProducts | Books |
---|---|---|---|
101 | 50 | 20 | 30 |
--------Update Statement--------
UPDATE Inventory
SET I.Bicycles = P.ProductCount
FROM Inventory I
JOIN Product P ON I.inventoryid = P.inventoryid
WHERE P.ProductName='Bicycles'
Instead of writing multiple update statements how to use one single query to update multiple columns?
1条答案
按热度按时间y4ekin9u1#
you can use this:
UPDATE Inventory SET I.Bicycles = P.ProductCount, I.another field= P.anotherfield, FROM Inventory I JOIN Product P ON I.inventoryid = P.inventoryid WHERE P.ProductName='Bicycles'
as many times as necessary, separated by commas, inside the set