2010-12-13 106 views
2

我有一個存儲過程,我將有一個輸出參數。查詢工作正常,當我在SQL Server Management Studio中運行查詢時,我得到了正確的答案。我的問題是分配答案給我的輸出參數。這裏是存儲過程:返回輸出參數的語法

ALTER PROCEDURE [dbo].[RDusp_Report_Impact] 
-- Add the parameters for the stored procedure here 
@SiteID int, 
@RiskCount int output 
AS 
BEGIN 
SET NOCOUNT ON; 



select sum(cnt) as mytotal from 
(
select count(Impact.Rating) as cnt from Impact, Likelihood, Exposure where 
Impact.SiteID=2 
and Exposure.SiteID = 2 and Impact.Rating > 3 and Likelihood.Rating > 3 
and Exposure.ImpactID = Impact.ImpactID and exposure.LikelihoodID = Likelihood.LikelihoodID 
) as c 


END 

我嘗試分配@RiskCount是在mytotal的價值,但它說,列不存在。我只想得到一個結果。不應該太困難,只是一個我無法得到的語法。謝謝。

回答

1

修改您的查詢像這樣(的關鍵部分是SELECT語句的第一行 - select @RiskCount = sum(cnt)

ALTER PROCEDURE [dbo].[RDusp_Report_Impact] 
-- Add the parameters for the stored procedure here 
@SiteID int, 
@RiskCount int output 
AS 
BEGIN 
    SET NOCOUNT ON; 

    select @RiskCount = sum(cnt) 
    from 
    (select count(Impact.Rating) as cnt 
    from Impact, Likelihood, Exposure 
    where Impact.SiteID=2 
    and Exposure.SiteID = 2 
    and Impact.Rating > 3 
    and Likelihood.Rating > 3 
    and Exposure.ImpactID = Impact.ImpactID 
    and exposure.LikelihoodID = Likelihood.LikelihoodID) as c 
END 

執行這樣的:

DECLARE @rc int 
EXEC [dbo].[RDusp_Report_Impact] 123, @rc output -- 123 is an example @SiteID value 
SELECT @rc 
+0

EXEC需要添加@SiteID值 – devio 2010-12-13 09:01:14

+0

@devio謝謝!我編輯了我的答案 – 2010-12-13 09:54:53

+0

@Marek如果我的select語句包含多於1列,即 select RiskCount = sum(cnt), Exposure.SiteI D 那麼什麼是語法 如果我的問題不是很清楚,請看看這個http://stackoverflow.com/questions/43039801/retrieving-output-parameter-value-from-two-internal-select -statements 我只是在等待迴應 – Meena 2017-03-27 12:23:45

相關問題