SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Difference between cursor and While loop


Difference between cursor and While loop

Author
Message
$t@r
$t@r
SSC-Addicted
SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)

Group: General Forum Members
Points: 411 Visits: 628
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??
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62317 Visits: 17954
$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 Modens 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)
Mansfield
Mansfield
Say Hey Kid
Say Hey Kid (700 reputation)Say Hey Kid (700 reputation)Say Hey Kid (700 reputation)Say Hey Kid (700 reputation)Say Hey Kid (700 reputation)Say Hey Kid (700 reputation)Say Hey Kid (700 reputation)Say Hey Kid (700 reputation)

Group: General Forum Members
Points: 700 Visits: 394
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
$t@r
$t@r
SSC-Addicted
SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)

Group: General Forum Members
Points: 411 Visits: 628
thanku all, can you explain me usage of cursors? in Which type of scenarios we use cursors?
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62317 Visits: 17954
$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 Modens 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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search