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 «««56789»»

Cursors for T-SQL Beginners Expand / Collapse
Author
Message
Posted Friday, May 27, 2011 9:37 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, December 16, 2011 1:05 PM
Points: 2, Visits: 54
battelofhalfwits (1/1/2009)
Wow...

Two things

1) Never use Cursors! Looping through a temp table takes less overhead than a Cursor; especially when you are working on a 24/7/365 server where you really need to watch your resources.



I want to echo this. I have encountered situations where there was no way around doing row-by-row inserts (database conversion / merger where a stored proc needed to be run to generate values for each line) but when it was first written with cursors our conversion process was going to take about 3 days to run. After replacing the cursors with temp tables and while loops the process was cut down to a few hours. I've never seen a situation where I could not replace a cursor and benefit.

D.
Post #1116345
Posted Friday, May 27, 2011 10:08 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, December 19, 2013 10:11 AM
Points: 17, Visits: 126
rothco (5/27/2011)
As a complete beginner I found the article badly mistitled. It was not Cursors for beginners but why not to use cursors for beginners.

A good starting point in an article for beginners is to assume that your reader knows nothing about the subject. This article singularly failed to do this.

About the title you are wrigth. Although the purpose off the article seemed explained in the article to me (a non-beginner), avoiding the beginners-reflex for cursors-usage to become a bad habit when there is no need to. (By the way, I started reading this article because I wanted to know if the article also would warn newbiees about the disadvantages off cursors. It did that indeed )

As a experienced user reading the discussion I thought, how the hell this level off discussion comes after a 'beginner-article'. Don't think many beginners will make it to this comment.
Post #1116374
Posted Friday, May 27, 2011 10:59 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, July 24, 2014 9:25 AM
Points: 136, Visits: 443
rothco (5/27/2011)
As a complete beginner I found the article badly mistitled. It was not Cursors for beginners but why not to use cursors for beginners.

A good starting point in an article for beginners is to assume that your reader knows nothing about the subject. This article singularly failed to do this.

I have to agree with you. I was kind of surprised when I saw the title myself, because I knew the discussion would be cursor bashing, but wasn't expecting the article itself to be.

That said, I have learned a lot here on replacements for cursors and other inefficient methods, and have implemented what I can understand and have the ability to. I will use Tally Tables for split functions as an example.
The reasons that I still use cursors are a tossup between using 2000 and that the majority of the time it is one time scripting



Director of Transmogrification Services
Post #1116399
Posted Friday, May 27, 2011 12:20 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 8:16 AM
Points: 1,035, Visits: 410
Two points:

First off the author, and a number of posts have suggested that "other SQL DBMS systems" are optimized for cursors and that it's only SQL Server that is not. Absolutely, positively, and categorically, this is FALSE! Oracle and DB2 are not magic, they don't somehow process cursors in such a way as to make them fine to use. A set based solution will outperform cursors in those platforms in almost direct proportion to how much it would outperform the same thing in SQL. Oracle in particular does their user community a GREAT disservice by not clarifying this issue.

Second, as much as I dislike cursors, and take pride in my ability to avoid them, there are some (very rare) cases where they are in fact the best solution. Categorically stating that they are always bad or always avoidable is not helpful.



/*****************

If most people are not willing to see the difficulty, this is mainly because, consciously or unconsciously, they assume that it will be they who will settle these questions for the others, and because they are convinced of their own capacity to do this. -Friedrich August von Hayek



*****************/
Post #1116454
Posted Friday, May 27, 2011 3:21 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, July 16, 2014 5:32 PM
Points: 388, Visits: 1,593
We've had to use cursors in generating notification emails using sp_send_dbmail. We have a list of 5 account execs that get notified when their data is loaded and ready for them to review. I'd love a set based version of sp_send_dbmail.

We also have a batch loading routine that uses a cursor to loop through a rules table in it to write a series of SQL statements that basic unpivot data from columns into records. The SQL statements themselves are set based inserts. It probably could be rewritten to use Unpivot along with some ugly case statements. But no one sees the point since it runs at most daily and isn't even a blip on our resources.

Thanks,
Mwise



Post #1116528
Posted Friday, May 27, 2011 11:15 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Yesterday @ 7:56 PM
Points: 36,775, Visits: 31,230
David Kuhl (5/27/2011)
battelofhalfwits (1/1/2009)
Wow...

Two things

1) Never use Cursors! Looping through a temp table takes less overhead than a Cursor; especially when you are working on a 24/7/365 server where you really need to watch your resources.



I want to echo this. I have encountered situations where there was no way around doing row-by-row inserts (database conversion / merger where a stored proc needed to be run to generate values for each line) but when it was first written with cursors our conversion process was going to take about 3 days to run. After replacing the cursors with temp tables and while loops the process was cut down to a few hours. I've never seen a situation where I could not replace a cursor and benefit.

D.


If you had simply changed your cursors to forward only, read only, static cursors, you would have achieved the same result.


--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 #1116592
Posted Friday, May 27, 2011 11:18 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Yesterday @ 7:56 PM
Points: 36,775, Visits: 31,230
Sorry... duplicate post 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 #1116593
Posted Friday, May 27, 2011 11:28 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Yesterday @ 7:56 PM
Points: 36,775, Visits: 31,230
pete.griffiths (5/27/2011)
For people not trained in using set based theory it is understandable to use a rbar approach because this is all that they know. But if you do know it then please use it!

Rant over ;)


Speaking of rants, I'd probably reword that to say something to the effect that if you don't know set-based theory, please stay away from my data and servers.


--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 #1116594
Posted Sunday, May 29, 2011 7:32 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Thursday, June 26, 2014 5:34 AM
Points: 897, Visits: 1,486
I haven't read all posts but...
DML is not Data Modification Language. It means Data Manipulation Language and the SELECT statement is also part of it.

Anyway great article. :)

I've never seen someone using a cursor to concatenate strings before though.

Best regards,




Best regards,

Andre Guerreiro Neto

Database Analyst
http://www.softplan.com.br
MCITPx1/MCTSx2
Post #1116774
Posted Tuesday, May 31, 2011 8:42 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, July 24, 2014 9:25 AM
Points: 136, Visits: 443
The SELECT statement may be part of DML, it all depends on the context and who makes the decision. A straight Read-Only SELECT does no manipulation so it would be outside of DML. It is when the SELECT is conjoined with INSERT, INTO or UPDATE that SELECT is considered to be in DML.



Director of Transmogrification Services
Post #1117468
« Prev Topic | Next Topic »

Add to briefcase «««56789»»

Permissions Expand / Collapse