Saturday, February 11, 2012

2005 Cursor Looping Issue

I've just begun to work in 2005 and am trying to run a cursor without any modification, which has proven to work in SQL 2000 and it's not looping.

the cursor has a few declared variables, running a select statement and assigning the returned value to the variables, then executing a sp using the variables as input. It is really written out textbook, for example:

declare
@.var1 int

declare cuMyCursor
Cursor For
(Select etc...)
Open cuMyCursor
Fetch Next from cuMyCursor
into @.Var1

while @.@.fetch_status = 0

begin
execute myStoredProc
@.var1

Fetch next from cuMyCursor
into @.Var1

end
close cuMyCusor
deallocate cuMyCursor

1 record affected

It will only execute the sp once and is not looping the cursor. I've checked the source data from the select statement and there are 600+ records to loop through before fetch next will not return a record. I literally ran this on a SQL2000 db with no problem, when i copy and paste it to run it in the SQL2005 db it will not loop.

Any insight would be helpful.

Thanks,
j.r.

I don't see anything obvious in the code. Would it be possible for you to post a sample that demonstrates this issue?|||I'm in the process of researching the issue further but i have discovered it is not the cursor at all that is causing the issue. The select statement that is used in the cursor is being run on the SQL2005 db and crossing to a linked SQL2000 db, THIS is where the issue lies. If i run just the select statement from a SQL2005 db that we created only one record is returned. When i run the same select statement from the Adventureworks db, the expected 677 records return. I'm looking into the differences between the sys.databases settings on the Adventureworks and our db thinking that the answer will be somewhere in the settings. When i find more symptoms i'll close this thread and open a new one.

J.R.

No comments:

Post a Comment