April 22, 2013 at 10:44 pm
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??
April 22, 2013 at 10:49 pm
$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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
April 22, 2013 at 10:52 pm
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
April 22, 2013 at 11:16 pm
thanku all, can you explain me usage of cursors? in Which type of scenarios we use cursors?
April 23, 2013 at 7:25 am
$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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply