2009-09-25 77 views

回答

2

我可以想到两种方法。其中第一个是当订单记录已经被插入到数据库中,您只是想更新其总价值:

UPDATE Order 
Set TotalPrice = NumberOfItems * 
       (SELECT Price FROM Food WHERE Food.FoodId = Order.FoodId) 

或者,你可以抓住食品项目的价格,当你插入的顺序到数据库表中:

-- Given: @FoodId and @NumberOfItems have been passed to this 
-- stored procedure as parameters 
DECLARE @price DECIMAL(10, 2) -- or whatever your price is defined to be 

SELECT @price = Price 
FROM Food 
WHERE FoodId = @FoodId 

INSERT INTO ORDER(FoodId, NumberOfItems, TotalPrice) 
VALUES 
(@FoodId, @NumberOfItems, @NumberOfItems * @Price) 
相关问题