简体   繁体   中英

SQL UPDATE with JOIN for WHERE Clause

Hopefully an easy question: I have two sql tables Items and BillOfMaterials

Items has fields ItemID and ItemCategory BillOfMaterials has fields ItemID and ComponentItemID

How can I do an UPDATE on BillOfMaterials to change the ComponentItemID where the ItemID has a certain category? eg

UPDATE BillOfMaterials
SET ComponentItemID = dbo.GetNewItemID(ComponentItemID)
WHERE ItemCategory = 1 <-- Magic join here to pull in ItemCategory

This should do it:

UPDATE b
SET ComponentItemID = dbo.GetNewItemID(ComponentItemID)
FROM BillOfMaterials b
INNER JOIN Items I on I.ItemID = b.ComponentItemID
WHERE i.ItemCategory = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM