用于WHERE子句的JOIN的SQL UPDATE

时间:2014-07-31 14:41:42

标签: sql sql-server

希望这是一个简单的问题:我有两个sqlItemsBillOfMaterials

Items包含字段ItemIDItemCategory BillOfMaterials包含字段ItemIDComponentItemID

如何在UPDATEBillOfMaterials更改ComponentItemID某个类别的ItemID? e.g。

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

1 个答案:

答案 0 :(得分:4)

这应该这样做:

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