Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««12345»»»

Cursor fetch loops endlessly Expand / Collapse
Author
Message
Posted Wednesday, November 20, 2013 4:11 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, September 29, 2014 10:46 AM
Points: 2,734, Visits: 943
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!


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.

No problem to be a old legacy the problem is to be poor designed
Post #1515979
Posted Wednesday, November 20, 2013 2:55 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 11:41 AM
Points: 35,609, Visits: 32,201
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!


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.

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


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.


--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1516230
Posted Thursday, November 21, 2013 6:55 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 6:02 AM
Points: 7,013, Visits: 7,180
Jeff Moden (11/19/2013)
I used to be a great VB6 programmer


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

p.s. Only joking Jeff, my old friend



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

Anon.

Post #1516415
Posted Friday, November 22, 2013 6:43 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, August 15, 2014 4:23 AM
Points: 3, Visits: 31
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.
Post #1516761
Posted Friday, November 22, 2013 7:28 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 6:02 AM
Points: 7,013, Visits: 7,180
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.

Post #1516779
Posted Friday, November 22, 2013 1:41 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 11:41 AM
Points: 35,609, Visits: 32,201
David Burrows (11/21/2013)
Jeff Moden (11/19/2013)
I used to be a great VB6 programmer


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

p.s. Only joking Jeff, my old friend


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.


--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1516928
Posted Friday, November 22, 2013 3:27 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:32 AM
Points: 7,161, Visits: 15,650
Jeff Moden (11/22/2013)
David Burrows (11/21/2013)
Jeff Moden (11/19/2013)
I used to be a great VB6 programmer


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

p.s. Only joking Jeff, my old friend


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.


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

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?
Post #1516955
Posted Friday, November 22, 2013 5:41 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 11:35 AM
Points: 4,482, Visits: 3,938
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 Are you a bad VB6 programmer now
And after everything you said about CLR sheesh

p.s. Only joking Jeff, my old friend


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.


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

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.



Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Post #1516975
Posted Friday, January 10, 2014 12:08 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, March 2, 2014 10:27 AM
Points: 6, 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.
Post #1529925
Posted Friday, January 10, 2014 12:40 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:56 AM
Points: 20,815, Visits: 32,748
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.



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)
Post #1529943
« Prev Topic | Next Topic »

Add to briefcase «««12345»»»

Permissions Expand / Collapse