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)