开发者

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 @开发者_Go百科max = 0
exec @max = DMM @max out
select @max as MaxValue


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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜