我必须使用由 3 个表的连接返回的值来更新一个字段。
例子:
select im.itemid ,im.sku as iSku ,gm.SKU as GSKU ,mm.ManufacturerId as ManuId ,mm.ManufacturerName ,im.mf_item_number ,mm.ManufacturerID from item_master im, group_master gm, Manufacturer_Master mm where im.mf_item_number like 'STA%' and im.sku=gm.sku and gm.ManufacturerID = mm.ManufacturerID and gm.manufacturerID=34
我想用在上述条件下加入的其他值来更新mf_item_number表的字段值。item_master
mf_item_number
item_master
如何在 MS SQL Server 中执行此操作?
UPDATE im SET mf_item_number = gm.SKU --etc FROM item_master im JOIN group_master gm ON im.sku = gm.sku JOIN Manufacturer_Master mm ON gm.ManufacturerID = mm.ManufacturerID WHERE im.mf_item_number like 'STA%' AND gm.manufacturerID = 34
说清楚…UPDATE子句可以引用子句中指定的表别名FROM。所以im在这种情况下是有效的
UPDATE
FROM
im
UPDATE A SET foo = B.bar FROM TableA A JOIN TableB B ON A.col1 = B.colx WHERE ...