Saturday, February 25, 2012

Retrieve multiple variables from Stored Procedure (SQLHelper)

Hi all,

I am using SQLHelper to run a Stored Procedure.
The Stored Procedure returns 3 variables:

ie:


SELECT @.Hits = COUNT(DISTINCT StatID) FROM Stats

...etc...

The SP is currently called as below:


SqlParameter[] sqlParams = new SqlParameter[]
{
new SqlParameter("@.FromDate", Request["FromDate"]),
new SqlParameter("@.ToDate", Request["ToDate"]),
};

My question is this: How do I retrieve these variables?

I know I need to declare a new SqlParameter and change it's Direction to Output but I am unsure of the exact syntax required to do this.

Thanks in advance,

PeteHi.

Could u please expain how you are returning the values.. all 3 as output parameter?

as the code will depend on the way the values are returned..|||my full Stored Procedure looks as so:


CREATE PROC GetGeneralStats
@.FromDate smalldatetime,
@.ToDate smalldatetime

AS

Declare @.uniqueHits int
Declare @.noOfSearches int

SELECT @.uniqueHits = COUNT(DISTINCT StatID) FROM Stats
WHERE (StatDate >= @.FromDate) AND (StatDate <= @.ToDate)

-- No of times Searchresults Shown
SELECT @.NoOfSearches = COUNT(SectionID) FROM Stats
WHERE (StatDate >= @.FromDate) AND (StatDate <= @.ToDate)
AND SectionID = 3

Go

Cheers,

Pete|||This procedure is retieving and storing values in 2 variables and returning nothing.

you need to add 2 output parameters to the procedure..

CREATE PROC GetGeneralStats

@.FromDate smalldatetime,

@.ToDate smalldatetime,

@.uniqueHits int OUTPUT,

@.noOfSearches int OUTPUT

AS

SELECT @.uniqueHits = COUNT(DISTINCT StatID) FROM Stats

WHERE (StatDate >= @.FromDate) AND (StatDate <= @.ToDate)

-- No of times Searchresults Shown

SELECT @.NoOfSearches = COUNT(SectionID) FROM Stats

WHERE (StatDate >= @.FromDate) AND (StatDate <= @.ToDate)

AND SectionID = 3

|||Hi,

Thanks for the reply.

As you said (and now having looked in the documentation) this is how it works but....

Im getting


Procedure 'GetGeneralStats' expects parameter '@.uniqueHits', which was not supplied.

So its thinking that I want them as Input variables? Even though I have them as:


@.uniqueHits int OUTPUT,
@.noOfSearches int OUTPUT

AS

pete|||Arrgghhh!!! Everywhere I look Im told to do it this way but Im getting this stupid error message!

Im tearing my hair out over this!!!!!!!

No comments:

Post a Comment