SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Cursor fetch loops endlessly


Cursor fetch loops endlessly

Author
Message
jcb
jcb
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2876 Visits: 994
Jeff Moden (11/19/2013)
jcb (11/19/2013)
VB6 argh... it smells as a old poor designed legacy app.


Hey, Hey, HEY! I'm old, I used to be a great VB6 programmer, I AM a legacy app, and I'm RIGHT HERE! :-D


As I used to be a programer in VB6, Deplhi 3,4,5,6,6.5,7, Turbo Pascal 7, Borland C 5, Basic and to be a great DOS tunning whore.
But that don't make me miss to use a mini PC with 8k memory or a 286 with no HD and two 8" floppy. :-D

No problem to be a old legacy the problem is to be poor designed Hehe
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87466 Visits: 41116
jcb (11/20/2013)
Jeff Moden (11/19/2013)
jcb (11/19/2013)
VB6 argh... it smells as a old poor designed legacy app.


Hey, Hey, HEY! I'm old, I used to be a great VB6 programmer, I AM a legacy app, and I'm RIGHT HERE! :-D


As I used to be a programer in VB6, Deplhi 3,4,5,6,6.5,7, Turbo Pascal 7, Borland C 5, Basic and to be a great DOS tunning whore.
But that don't make me miss to use a mini PC with 8k memory or a 286 with no HD and two 8" floppy. :-D

No problem to be a old legacy the problem is to be poor designed Hehe


Heh... I gave all that kind of stuff up in 2002 (well, except some of the DOS stuff) and haven't looked back. I did like my 8K of memory but that was only because I had twice as much as all my friends. :-D

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
David Burrows
David Burrows
SSCrazy Eights
SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)

Group: General Forum Members
Points: 9675 Visits: 9764
Jeff Moden (11/19/2013)
I used to be a great VB6 programmer


WHAT Exclamation Mark Are you a bad VB6 programmer now
And after everything you said about CLR sheesh Rolleyes

p.s. Only joking Jeff, my old friend :-D


Far away is close at hand in the images of elsewhere.

Anon.


Gavin Lock
Gavin Lock
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 36
Everyone's already discussed using set-based queries - so I won't say any more on that - but in the spirit of understanding why this is happening :-

Peter, check the indexes on the Addresses table on your production server. You might be bumping into the Halloween Problem.
I was able to repro your problem like this:


set nocount on;

create table Addresses (AccountID int identity, Country nvarchar(20) not null, lpn int null);
insert into Addresses(Country) values ('HR'), ('HR'), ('ZA');
create clustered index FooBar on Addresses(lpn); -- this breaks it

create table lpn(ID int identity, DeliveryID int);
go

declare @newLPN int;
declare @currentID int;
set @newLPN = 0;
set @currentID = 0;

DECLARE cursorlpn CURSOR FOR
select AccountID from Addresses
where country = 'HR';

open cursorlpn;
fetch next from cursorlpn into @currentID

while (@@fetch_status = 0)
begin
raiserror('AccountID = %i', 0, 0, @currentID) with nowait;

insert into lpn (deliveryID) values (0);
set @newLPN = scope_identity();
update Addresses set lpn = @newLPN where AccountID = @currentID;

fetch next from cursorlpn into @currentID
end

close cursorlpn
deallocate cursorlpn



Which outputs


AccountID = 1
AccountID = 2
AccountID = 1
AccountID = 2
AccountID = 1
AccountID = 2
AccountID = 1
...



Adding the FooBar clustered index means that each address row that is updated moves to the end of the table, so after updating row 2, the cursor finds row 1 again, and after updating it a second time it then finds row 2 again, and so on.
David Burrows
David Burrows
SSCrazy Eights
SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)

Group: General Forum Members
Points: 9675 Visits: 9764
Gavin Lock (11/22/2013)
You might be bumping into the Halloween Problem.
I was able to repro your problem like this:


Changing the cursor to STATIC will stop your query looping.


Far away is close at hand in the images of elsewhere.

Anon.


Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87466 Visits: 41116
David Burrows (11/21/2013)
Jeff Moden (11/19/2013)
I used to be a great VB6 programmer


WHAT Exclamation Mark Are you a bad VB6 programmer now
And after everything you said about CLR sheesh Rolleyes

p.s. Only joking Jeff, my old friend :-D


BWAAA-HAAA!!!! I might be terrible at it now. After my old manager directed me to style a "warning" field with a Pale Yellow background/White lettering and a "danger" field with a Fusia background and a particular Pink lettering, I had all knowledge of front end and business layer programming surgically and chemically (mostly alchohol) removed. :-P

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Matt Miller (4)
Matt Miller (4)
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12455 Visits: 18576
Jeff Moden (11/22/2013)
David Burrows (11/21/2013)
Jeff Moden (11/19/2013)
I used to be a great VB6 programmer


WHAT Exclamation Mark Are you a bad VB6 programmer now
And after everything you said about CLR sheesh Rolleyes

p.s. Only joking Jeff, my old friend :-D


BWAAA-HAAA!!!! I might be terrible at it now. After my old manager directed me to style a "warning" field with a Pale Yellow background/White lettering and a "danger" field with a Fusia background and a particular Pink lettering, I had all knowledge of front end and business layer programming surgically and chemically (mostly alchohol) removed. :-P


Lordy - what a color combo! ach! I've had managers like that too :-P

But still - this one took me back. I had occasion to see the last of one of my legacy VB6 apps finally graduate up to .NET earlier this year, so I think I am finally out of that mess :-) That's another 8K of memory I can free up for relevant info.

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Ed Wagner
Ed Wagner
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16774 Visits: 10083
Matt Miller (#4) (11/22/2013)
Jeff Moden (11/22/2013)
David Burrows (11/21/2013)
Jeff Moden (11/19/2013)
I used to be a great VB6 programmer


WHAT Exclamation Mark Are you a bad VB6 programmer now
And after everything you said about CLR sheesh Rolleyes

p.s. Only joking Jeff, my old friend :-D


BWAAA-HAAA!!!! I might be terrible at it now. After my old manager directed me to style a "warning" field with a Pale Yellow background/White lettering and a "danger" field with a Fusia background and a particular Pink lettering, I had all knowledge of front end and business layer programming surgically and chemically (mostly alchohol) removed. :-P


Lordy - what a color combo! ach! I've had managers like that too :-P

But still - this one took me back. I had occasion to see the last of one of my legacy VB6 apps finally graduate up to .NET earlier this year, so I think I am finally out of that mess :-) That's another 8K of memory I can free up for relevant info.

I still have VB5 and VB6 apps in production. I've moved on from that company, so there's virtually no chance of me having to rewrite them. Someone else can have at it, but there's no need as long as the VB runtimes still function on whatever server they're running on. :-D


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
mwitthoft
mwitthoft
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 19
I am battling a similar "self-restarting cursor" . The defect
(a) occurs 100% on one server
(b) occurs 0% on another server

Pseudocode
Declare cursor as..
Open cursor
Fetch next
while @@FetchStatus
do stuff
FetchNext
--** right here, if it hits EndOfData, it just happily fetches First Row of Cursor and rolls on.
end
close cursor

Seems like there is a new bug in SQL Server, and it depends on some unknown aspect of how the server is configured.
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39948 Visits: 38564
mwitthoft (1/10/2014)
I am battling a similar "self-restarting cursor" . The defect
(a) occurs 100% on one server
(b) occurs 0% on another server

Pseudocode
Declare cursor as..
Open cursor
Fetch next
while @@FetchStatus
do stuff
FetchNext
--** right here, if it hits EndOfData, it just happily fetches First Row of Cursor and rolls on.
end
close cursor

Seems like there is a new bug in SQL Server, and it depends on some unknown aspect of how the server is configured.


First, there isn't enough to even try to help here. Second, I would start looking at finding a way to eliminate the cursor and replace it with a set-based process.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search