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 2008 Forums
 Transact-SQL (2008)
 Are Cursors recomended?

Author  Topic 

learntsql

524 Posts

Posted - 2012-04-18 : 05:00:46
Hi All,

Please guide me about cursors usage.
1.Are the cursors usefull?
2.Are there any alternatives for cursor?
3.Are there any cases where we have to go with the cursor?
TIA.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-04-18 : 05:23:21
http://lmgtfy.com/?q=sql+pros+and+cons+of+cursors


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

sql-programmers
Posting Yak Master

190 Posts

Posted - 2012-04-18 : 05:25:47
If possible dont use CURSORs. It will make performance issue. You can use WHILE Loop instead of using cursors.

Based functionality it will differ to use cursor. at that time try to use WHILE loop.

Some sample for WHILE LOOP
http://arrao4u.wordpress.com/2010/01/26/avoid-cursors-use-while-loop-in-sql-server/
http://blog.sqlauthority.com/2007/10/24/sql-server-simple-example-of-while-loop-with-continue-and-break-keywords/


SQL Server Programmers and Consultants
http://www.sql-programmers.com/
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-04-18 : 07:52:04
1. No
2. SQL
3. No
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2012-04-18 : 10:02:31
>> You can use WHILE Loop instead of using cursors.

true, but this won't help. if you are doing things row-by-row, perf will generally suck no matter what programming construct you use.


elsasoft.org
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-04-18 : 10:13:50
http://www.imdb.com/title/tt0084978/

Together, Walter and Automan along with Cursor, a small floating droid that can create any object Automan needs, battle the criminal elements of the city.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-04-18 : 10:38:36
quote:
Originally posted by sql-programmers

If possible dont use CURSORs. It will make performance issue. You can use WHILE Loop instead of using cursors.

Based functionality it will differ to use cursor. at that time try to use WHILE loop.

Some sample for WHILE LOOP
http://arrao4u.wordpress.com/2010/01/26/avoid-cursors-use-while-loop-in-sql-server/
http://blog.sqlauthority.com/2007/10/24/sql-server-simple-example-of-while-loop-with-continue-and-break-keywords/


SQL Server Programmers and Consultants
http://www.sql-programmers.com/



jezemine already commented but not strongly enough.

THERE IS NO FUNCTIONAL DIFFERENCE (that matters)

Don't do row by bloody row. I can't think of a single case or problem presented here or anywhere else where there wasn't a better way of doing it.

Not everything boils down to a pure set based solution but there is always a better alternative than a cursor.

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -