返回输出参数的语法

时间:2010-12-13 07:12:52

标签: sql sql-server tsql stored-procedures

我有一个存储过程,我将有一个输出参数。查询有效,当我在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 个答案:

答案 0 :(得分: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