2010-12-13 3 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文の1行目である(このようなあなたのクエリを変更 - 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

+0

@devioありがとう!私の回答を編集しました –

+0

私のselectステートメントに2つ以上の列が含まれている場合、つまり を選択した場合 RiskCount = sum(cnt)、 Exposure.SiteI D 私の質問があまり明確でない場合は、どのような構文の をshoyldしてくださいこれを見てくださいくださいhttp://stackoverflow.com/questions/43039801/retrieving-output-parameter-value-from-two-internal-select -statements 私は応答を待っています – Meena

関連する問題