产品表
create table Products
(
ProductId nvarchar(8) primary key not null,
Quantity int
)ProductId Quantity
------------------------
0001 5
0002 3
0003 1
0004 10注:该表不是产品表,是从订单表中读出来的结果。循环上表的行,取每行的ProductId和Quantity做为sql语句:update Product set Quantity = Quantity + @Quantity where ProductId = @ProductId中的参数。
create table Products
(
ProductId nvarchar(8) primary key not null,
Quantity int
)ProductId Quantity
------------------------
0001 5
0002 3
0003 1
0004 10注:该表不是产品表,是从订单表中读出来的结果。循环上表的行,取每行的ProductId和Quantity做为sql语句:update Product set Quantity = Quantity + @Quantity where ProductId = @ProductId中的参数。
set
Quantity=a.Quantity + b.Quantity
from
Product a,
(select ProductId,sum(Quantity) as Quantity from 订单表 group by ProductId) b
where
a.ProductId=b.ProductId
直接JOIN不就完事了吗UPDATE Product
SET Quantity=a.Quantity+b.Quantity
FROM Product a INNER JOIN Products b
ON a.ProductID=b.ProductID