Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Cursor or No Cursors

Author  Topic 

vgr.raju
Yak Posting Veteran

95 Posts

Posted - 2007-12-07 : 02:33:36
Hello Experts,
I have been faced with the same issue regarding whether to use cursor or while loops.
Most of the scenarios,I have to fetch the data from table and then execute the stored proc passing the table data as Stored Procedure parameters.
I can get it done using cursors as well as while loops.
I have looked at the query plans and gathered some other I/O Statistics and I did not see any significant differnces.
Overall attitude in the company is to avoid the cursor if possible.
I can certainly avoid the cursor but I am not finding any concrete reasons for avoiding the cursors.

IF some one can shed light on where the cursors would be more efficient than while loops ,that would be greatly appreciated.
Thanks,
Raju

Thanks!
Raju
http://rajusqlblog.blogspot.com/

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2007-12-07 : 04:14:55
Are you passing the rows one at a time to the calling procedure? If so, is there any particular reason for this?

Cursors are generally slower than set based solutions, there are obviously exceptions to this, and a inefficient query with a while is one of the exceptions.

Post your code and you will get more help.
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2007-12-07 : 04:15:31
Good article on http://www.sqlteam.com/article/cursor-performance

Jack Vamvas
--------------------
Search IT jobs from multiple sources- http://www.ITjobfeed.com
Go to Top of Page

vgr.raju
Yak Posting Veteran

95 Posts

Posted - 2007-12-11 : 03:57:08
Thank you all.My scenario is not set based and I have to iterate through the data based on values and Cursors make more sense to use.
Thanks,
Raju

Thanks!
Raju
http://rajusqlblog.blogspot.com/
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-12-11 : 04:08:41
Well, if you say you must iterate all records one by one we believe you.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2007-12-11 : 04:21:40
quote:
Originally posted by Peso

Well, if you say you must iterate all records one by one we believe you.



E 12°55'05.25"
N 56°04'39.16"



You might, I on the other hand have various resevations.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-12-11 : 04:28:31
I realize the irony in my response was not easily recognized.
See http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=93911


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-12-11 : 05:39:30
Also refer http://sqlblog.com/blogs/hugo_kornelis/archive/2007/11/28/poor-men-see-sharp-ndash-more-cursor-optimization.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -