Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Friends,

In one website I have learnt that, using Cursor is not good practice.
Is it so. If yes then what is the alternate to use Cursor.

Regards,
AP
Posted
Comments
Sergey Alexandrovich Kryukov 26-May-12 0:28am    
I would rather agree with this statement, but the question "what is the alternative to using cursor?" it not quite valid. The trivial answer would be "not using cursor". :-)
This question is not quite valid, because you first need to point out the particular situation (problem, application) which as you think requires the use of cursor and ask about the alternative. Without such example, there is nothing to discuss.
--SA
Arunprasath Natarajan 26-May-12 8:23am    
Thank you sir, I will come back to you with a eg.
Sandeep Mewara 29-May-12 1:14am    
:)

Cursors take up memory and create locks. They incur a performance penalty most of the times.
AFAIK, Alternative suggested is Table variable in a while loop.

See this: Increase your SQL Server performance by replacing cursors with set operations[^]
Cursor Performance[^]


Similar discussions here:
http://stackoverflow.com/questions/287445/why-do-people-hate-sql-cursors-so-much[^]
Why is it considered bad practice to use cursors in SQL Server?[^]
MS SQL Server - When is a CURSOR good?[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 28-May-12 19:38pm    
Good answer, a 5, but please also see my comment to the question.
--SA
Sandeep Mewara 29-May-12 1:14am    
Thanks SA.
The problem with cursors is that they are resource consuming and slow. So whenever possible you should use set based approach i.e. SELECT statement.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-May-12 19:38pm    
Agree, a 5, but please also see my comment to the question.
--SA
Wendelius 1-Jun-12 0:46am    
Thanks :) Yes good comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900