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

Difference between cursor and While loop Expand / Collapse
Author
Message
Posted Monday, April 22, 2013 10:44 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, March 5, 2014 10:24 PM
Points: 48, Visits: 370
Hi all,


Can any one explain me What is the difference between Cursor and While Loop? I have read about two concepts but end up with confusion..Which one gives better performance??
Post #1445265
Posted Monday, April 22, 2013 10:49 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 4:03 PM
Points: 12,928, Visits: 12,347
$w@t (4/22/2013)
Hi all,


Can any one explain me What is the difference between Cursor and While Loop? I have read about two concepts but end up with confusion..Which one gives better performance??


Neither of them is preferred in most situations. They both have their purpose but it is far better to use set based approaches instead of looping.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1445268
Posted Monday, April 22, 2013 10:52 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 2:20 PM
Points: 364, Visits: 384
A cursor loops through each row of a SELECT statement, and runs some code.
A while loop is a generic loop construct.

However, SQL server works best with set-based queries, so neither will be efficient.

If there is a specific requirement that you would like help architecturing let us know

EDIT: beaten to it by a few minutes
Post #1445269
Posted Monday, April 22, 2013 11:16 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, March 5, 2014 10:24 PM
Points: 48, Visits: 370
thanku all, can you explain me usage of cursors? in Which type of scenarios we use cursors?
Post #1445272
Posted Tuesday, April 23, 2013 7:25 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 4:03 PM
Points: 12,928, Visits: 12,347
$w@t (4/22/2013)
thanku all, can you explain me usage of cursors? in Which type of scenarios we use cursors?


For maintenance tasks that require looking at all tables or maybe even all databases. Maybe if you need to send customized email messages to a whole list of people (very possibly should use something besides t-sql for this anyway). You don't need cursors for things like inserts, updates, etc... These types of operations can almost always be done in a set based manner.

While loops fall into the same category. There are very few times they are needed.

I am guessing that you are fairly new to sql. I would recommend that you pretend you don't know about cursors or while loops. SQL requires a different mindset. You have to start thinking of data in sets instead of rows. Then when you want to modify data you start to think about what you want to do to a column instead of a row.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1445409
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse