2009-09-25 34 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) 
相關問題