Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

recursive stored procedure w/cursor?

Status
Not open for further replies.

BeanDog

Programmer
Jul 15, 2000
60
US
I know it sounds kind of ridiculous, but nevertheless I do have to have a stored procedure that calls itself. It has a cursor active in it when it calls itself. When the second instance of the procedure runs, it gives me (obviously) a cursor already exists error. Can anyone think of a way around this?

Here's the full code:

Alter Procedure qrysMaxOverlaps
(
@pAppointmentID int,
@pOverlaps int OUTPUT
)
As
/*
This query returns the maximum number of appointments that begin during this one.
*/
declare @sTime datetime
declare @eTime datetime
declare @Master int
declare @date datetime
declare @tempID int
declare @tempint int
declare cur CURSOR FOR
SELECT starttime, endtime, masterID, date FROM tblsAppointment WHERE appointmentID = @pAppointmentID

OPEN cur
FETCH cur INTO @sTime, @eTime, @Master, @date
CLOSE cur
DEALLOCATE cur

/*
we have to loop through all the appointments that start during this one and call this
procedure. Whenever one produces a higher value, @pOverlaps will be set to that value.
*/
declare cur CURSOR FOR
SELECT appointmentID FROM tblsAppointment
WHERE(
masterID=@Master AND
date=@date AND
starttime > @sTime AND
starttime < @eTime
)

OPEN cur

set @tempID = 0
set @tempint = 0
set @pOverlaps = 0
FETCH cur INTO @tempID
while (@@FETCH_STATUS=0)
BEGIN
FETCH cur INTO @tempID
EXECUTE qrysMaxOverlaps @tempID, @tempint
if @tempint > @pOverlaps
BEGIN
set @pOverlaps = @tempint
END
END
CLOSE cur
DEALLOCATE cur


return


~BenDilts( void );

~BenDilts( void );
benbeandogdilts@cs.com
Long-time BASIC game programmer, Internet programmer and C++/DirectX of late.
 
Try to declare the cursor as LOCAL. The default is GLOBAL so it exists within the connection session.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top