2013-03-01 76 views
1

我有在SQL乘兩列存儲過程

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.

請幫我解決這個問題

回答

7

你不需要做一個子查詢,以獲得兩列的產品,只需要直接相乘,

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