C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » SQL Server »

Disadvantages of Cursor?


Posted Date: 07 Nov 2009      Posted By: Syed Shakeer Hussain       Member Level: Diamond     Points: 1   Responses: 3



What are the Disadvantages of Cursor in sqlserver?Plz reply in simple points


Thanks & Regards!
Syed Shakeer Hussain





Responses

Author: Giribabu    07 Nov 2009Member Level: GoldRating:     Points: -20

What are cursors? Explain different types of cursors. What are the
disadvantages of cursors? How can you avoid cursors?

Cursors allow row-by-row prcessing of the resultsets.

Types of cursors: Static, Dynamic, Forward-only, Keyset-driven. See
books online for more information.

Disadvantages of cursors: Each time you fetch a row from the cursor,
it results in a network roundtrip, where as a normal SELECT query
makes only one rowundtrip, however large the resultset is. Cursors are
also costly because they require more resources and temporary storage
(results in more IO operations). Furthere, there are restrictions on
the SELECT statements that can be used with some types of cursors.

Most of the times, set based operations can be used instead of
cursors. Here is an example:

If you have to give a flat hike to your employees using the following
criteria:

Salary between 30000 and 40000 -- 5000 hike
Salary between 40000 and 55000 -- 7000 hike
Salary between 55000 and 65000 -- 9000 hike

In this situation many developers tend to use a cursor, determine each
employee's salary and update his salary according to the above
formula. But the same can be achieved by multiple update statements or
can be combined in a single UPDATE statement as shown below:

UPDATE tbl_emp SET salary =
CASE WHEN salary BETWEEN 30000 AND 40000 THEN salary + 5000
WHEN salary BETWEEN 40000 AND 55000 THEN salary + 7000
WHEN salary BETWEEN 55000 AND 65000 THEN salary + 10000
END

Another situation in which developers tend to use cursors: You need to
call a stored procedure when a column in a particular row meets
certain condition. You don't have to use cursors for this. This can be
achieved using WHILE loop, as long as there is a unique key to
identify each row. For examples of using WHILE loop for row by row
processing, check out the 'My code library' section of my site or
search for WHILE.

--Giri



Author: Deepika Haridas    07 Nov 2009Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

Hello,

You can check my post for the same

http://www.dotnetspider.com/resources/27447-Cursors.aspx



Thanks & Regards,
Deepika
Editor

If U want to shine like a SUN..First U have to burn like the SUN!!
Need a Guide? Join my mentor program..



Author: Mayura Sangeene    07 Nov 2009Member Level: BronzeRating: 2 out of 52 out of 5     Points: 2

If possible, avoid using SQL Server cursors. They generally use a lot of SQL Server resources and reduce the performance and scalability of your applications. If you need to perform row-by-row operations, try to find another method to perform the task. Some options are to perform the task at the client, use tempdb tables, use derived tables, use a correlated subquery, or use the CASE statement. More often than not, there are non-cursor techniques that can be used to perform the same tasks as a SQL Server cursor.

Regards
Mayura Sangeene



Post Reply
You must Sign In to post a response.
Next : Update Query
Previous : Joining table
Return to Discussion Forum
Post New Message
Category: SQL Server

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use