Shine
Shine

Reputation: 1423

Error with stored procedure

I have declared a cursor to fetch max marks from table out side the procedure. Fetching the value through that cursor inside procedure. But procedure returning zero. Please help me finding the error.

I am using the following code.

Declare Cur Cursor for select max(marks) from stu

Procedure

Alter procedure DMM(@max int output)
as
open Cur
Fetch from Cur into @max
close Cur

Code used to execute the procedure

declare @max int
set @max = 0
exec @max = DMM @max out
select @max as MaxValue

Upvotes: 1

Views: 96

Answers (2)

Andreas Ågren
Andreas Ågren

Reputation: 3929

The problem is that you are executing the procedure incorrectly, here's how you should do it:

declare @max int
set @max = 0
exec DMM @max output
select @max as MaxValue

Other than that I agree with marc_s, why use a cursor?

Upvotes: 1

RB.
RB.

Reputation: 37212

You're problem is that you are using the same variable to store the output parameter as you are using to get the procedure's return code. Therefore, your return-code (0) is overwriting your output.

You should call your procedure as shown:

declare @max int
declare @returnCode int

set @max = 0
exec @returnCode = DMM @max out
select @max as MaxValue

Upvotes: 1

Related Questions