在Sql Stored过程中将两列相乘

时间:2013-03-01 07:37:38

标签: asp.net sql sql-server stored-procedures

我有一张表库存

StockID   ItemID   Quantity   Price

现在我想选择一些像这样的数据

StockID   ItemID   Quantity Price   TotalPrice(Quantity*Price)

我试过这个

ALTER PROC [dbo].[SelectItemStock] 
@Item   int
as
BEGIN
    SELECT  Stock.*, (SELECT Quantity*Price FROM Stock) AS TotalPrice
    FROM    Stock
    WHERE   ItemID = @Item
END

但它给了我错误Subquery returned more than 1 value.

请帮我解决这个问题

1 个答案:

答案 0 :(得分:7)

您不需要执行子查询来获取两列的乘积,只需将其直接乘以,

ALTER PROC [dbo].[SelectItemStock] 
@Item   int
as
BEGIN
    SELECT  Stock.*, 
            (Quantity*Price) AS TotalPrice
    FROM    Stock
    WHERE   ItemID = @Item
END