Monday, March 12, 2012

Nested Cursor

I think I am getting an endless loop here... anyone know how to fix it?

***********************

CREATE PROCEDURE TrigSendPreNewIMAlertP2
@.REID int

AS

Declare @.RRID int
Declare @.ITID int
Declare @.FS2 int
Declare @.FS1 int

Declare crReqRec cursor for
select RRID from RequestRecords where REID = @.REID and RRSTatus = 'IA' and APID is not null
open crReqRec
fetch next from crReqRec
into
@.RRID

Declare crImpGrp cursor for
select ITID from RequestRecords where RRID = @.RRID
open crImpGrp
fetch next from crImgGrp
into
@.ITID

while @.@.fetch_status = 0
select @.FS1 = @.@.Fetch_Status

EXEC TrigSendNewIMAlertP2 @.ITID

FETCH NEXT FROM crImpGrp
into
@.ITID

close crImpGrp
deallocate crImpGrp

while @.@.Fetch_Status = 0
select @.FS2 = @.@.Fetch_Status

FETCH NEXT FROM crReqRec
into
@.RRID

close crReqRec
deallocate crReqRec
GOi think i had replied to you earlier with a similar question about cursors ? coz i recollect you did the same thing at that time..

heres a good template for a cursor..you should be able to figur eit out pretty easily for a nested cursor too :


DECLARE rs CURSOR
LOCAL
FORWARD_ONLY
OPTIMISTIC
TYPE_WARNING
FOR SELECT .........
OPEN rs
fetch next from rs into .....
WHILE ( @.@.FETCH_STATUS = 0 )
begin

--do your processing here...in your case declare the nested cursor here

FETCH NEXT FROM rs INTO ......
END

close rs
deallocate rs

hth

No comments:

Post a Comment