如何根据SQL Server Express中的查询结果更新列值

时间:2012-06-06 07:10:58

标签: sql triggers sql-server-express

我有两张表ProductProductInventory。我创建了一个FULL Join并创建了一个查询,它为我提供了Makeflag所有数量已经变为零的产品。

如何操作输出,以便我可以将所有数量已变为0的产品的Makeflag值更改为1?

下面的查询实际上影响了Makeflag的所有值,而不仅仅是数量已经为零的产品。

我做错了什么......请帮忙!

Use AdventureWorks2008R2

Select 
    Quantity, MakeFlag 
from Production.ProductInventory
FULL Join AdventureWorks2008R2.Production.Product
ON ProductInventory.ProductID = Product.ProductID where Quantity = 0

update AdventureWorks2008R2.Production.Product set MakeFlag = 1

2 个答案:

答案 0 :(得分:1)

这应该可以解决问题

update AdventureWorks2008R2.Production.Product set MakeFlag = 1 
WHERE ProductID IN (
  SELECT ProductID FROM from Production.ProductInventory where Quantity = 0
)

答案 1 :(得分:0)

UPDATE AdventureWorks2008R2.Production.Product SET MakeFlag = 1
WHERE AdventureWorks2008R2.Production.Product.ProductId IN
(SELECT Product.ProductID from Production.ProductInventory FULL Join AdventureWorks2008R2.Production.Product ON ProductInventory.ProductID = Product.ProductID where Quantity = 0)