Search:     Advanced search
Browse by category:
Glossary | Contact Us

Database Knowledge Base / MS SQL Server /

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

Add comment
Views: 245
Votes: 1
Comments: 0

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.

Others in this Category
document How to change the owner of all tables in a database?
document how to build connection string for MS SQL Server?
document How we can back up all databases in SQL Server
document If the TempDB Database is deleted from MS-SQL Server what will happen..?
document How to insert a not null column in an existing table with records..?
document If a table is deleted, what will happen for the Stored procedures and Views, which that table reffered..?
document Is there an easy way to loop through every stored procedure in a database and create a file containing all of the SP code?
document What is denormalization and when would you go for it?
document How do you implement one-to-one, one-to-many and many-to-many relationships while designing tables?
document What's the difference between a primary key and a unique key?
document What are user defined datatypes and when you should go for them?
document What is bit datatype and what's the information that can be stored inside a bit column?
document How we can determine which version of SQL Server 2005 is running?
document How to determine which version of SQL Server 2000 is running
document Can you have a nested transaction?
document What is an extended stored procedure? Can you instantiate a COM object by using T-SQL?
document You have just had to restore from backup and do not have any control files. How would you go about bringing up this database?
document Is length of sql query limited in MS_SQL Server ?
document Where we can find Free MS SQL Server System Table chart or map ?
document I'm using SQL Server 2005. Do I need to get Enterprise Manager to administer the app through a hosted server? What do I need if not EM?
document How do I restict clients by IP Address?
document How do I perform encryption with SQL Server?
document What is SQLPing and how does it work?
document How can I hide my SQL Server 2000 installations from SQLPing?
document How can I keep the 'sa' account from reading my confidential data?
document Why is SQL Server security important?
document What different network protocol libraries should I use?
document What different network protocol libraries should I use?
document What are the various security modes for SQL Server and how do they work?
document What are some things I can do to secure my SQL Server?
document What could happen if the 'sa' account is compromised?
document Does MySQL 5.0 provide any new features/capabilities for MySQL Cluster?
document Can you use MySQL GUI Tools to build Stored Procedures and Views?
document How does PostgreSQL compare to other DBMSs?
document How we can perform MINUS (Oracle) Or EXCEPT (Maxdb) in MS Access?
document What is normalization?
document What is a Stored Procedure?
document Can you give an example of Stored Procedure?
document What is a trigger?
document What is a view?
document What is an Index?
document What are the types of indexes available with SQL Server?
document What is the basic difference between clustered and a non-clustered index?
document What are cursors?
document When do we use the UPDATE_STATISTICS command?
document Which TCP/IP port does SQL Server run on?
document From where can you change the default port?
document Can you tell me the difference between DELETE & TRUNCATE commands?
document Can we use Truncate command on a table which is referenced by FOREIGN KEY?
document What is the use of DBCC commands?
document Can you give me some DBCC command options?
document What command do we use to rename a db?
document Well sometimes sp_reanmedb may not work you know because if some one is using the db it will not accept this command so what do you think you can do in such cases?
document What is the difference between a HAVING CLAUSE and a WHERE CLAUSE?
document What do you mean by COLLATION?
document What is a Join in SQL Server?
document Can you explain the types of Joins that we can have with Sql Server?
document When do you use SQL Profiler?
document What is a Linked Server?
document Can you link only other SQL Servers or any database servers such as Oracle?
document Which stored procedure will you be running to add a linked server?
document What are the OS services that the SQL Server installation adds?
document Can you explain the role of each service?
document How do you troubleshoot SQL Server if its running very slow?
document Lets say due to N/W or Security issues client is not able to connect to server or vice versa. How do you troubleshoot?
document What are the authentication modes in SQL Server? -
document Where do you think the users names and passwords will be stored in sql server?
document What is log shipping? Can we do logshipping with SQL Server 7.0
document Let us say the SQL Server crashed and you are rebuilding the databases including the master database what procedure to you follow?
document Let us say master db itself has no backup. Now you have to rebuild the db so what kind of action do you take?
document What is BCP? When do we use it?
document What should we do to copy the tables, schema and views from one SQL Server to another?
document What are the different types of joins and what does each do?
document What is a sub-query? When would you use one?
document Hacking SQL Server!!!
document When do you use SQL Profiler?
document What is a Linked Server?
document Can you link only other SQL Servers or any database servers such as Oracle?
document Which stored procedure will you be running to add a linked server?
document What are the OS services that the SQL Server installation adds?
document Can you explain the role of each service?
document How do you troubleshoot SQL Server if its running very slow?
document What are the authentication modes in SQL Server?
document Where do you think the users names and passwords will be stored in sql server?
document What is log shipping?
document What is BCP?
document What is Data Mapping
document What is an Ad Hoc Query?
document How to identify your SQL Server version and edition?
document what is the method to change location of tempdb?
document How to know which index a table is using?
document What is Data page?
document Define Disaster recovery planning.
document define term Minimally logged operations (bulk load operations)
document What is Filegroup?
document What is Log sequence number (LSN)?
document what is an Extent?
document how we can change ownership in SQL Server?



RSS