Theme-Logo
  •   .Net
    •   C Sharp(C#)
    •   Web API
    •   Micro Services
    •   ASP.Net
    • ASP.Net MVC
    • .Net Core
  •   Database
    • SQL Server
    • Oracle
    • PostgreSQL
  •   jQuery
    • jQuery Tutorials
    • jQuery Plugins
    • jQuery UI
    • More on jquery
  •   Tutorials
    • Microservices Tutorials
    • DotNet Core Tutorials
    • PostgreSql Tutorials

Cursor in SQL Server

Operations in a relational database act on a complete set of rows. For example, the set of rows returned by a SELECT statement consists of all the rows that satisfy the conditions in the WHERE clause of the statement. This complete set of rows returned by the statement is known as the result set. Applications, especially interactive online applications, cannot always work effectively with the entire result set as a unit. These applications need a mechanism to work with one row or a small block of rows at a time. Cursors are an extension to result sets that provide that mechanism.

Cursors extend result processing by:

  • Allowing positioning at specific rows of the result set.
  • Retrieving one row or block of rows from the current position in the result set.
  • Supporting data modifications to the rows at the current position in the result set.
  • Supporting different levels of visibility to changes made by other users to the database data that is presented in the result set.
  • Providing Transact-SQL statements in scripts, stored procedures, and triggers access to the data in a result set.
  • Client cursors

    Client cursors are implemented internally by the SQL Server Native Client ODBC driver and by the DLL that implements the ADO API. Client cursors are implemented by caching all the result set rows on the client. Each time a client application calls an API cursor function, the SQL Server Native Client ODBC driver or the ADO DLL performs the cursor operation on the result set rows cached on the client.

    Type of Cursors

    SQL Server supports four cursor types.

    1. Forward-only Cursors

    A forward-only cursor is specified as FORWARD_ONLY and READ_ONLY and does not support scrolling. These are also called firehose cursors and support only fetching the rows serially from the start to the end of the cursor. The rows are not retrieved from the database until they are fetched. The effects of all INSERT, UPDATE, and DELETE statements made by the current user or committed by other users that affect rows in the result set are visible as the rows are fetched from the cursor.

    2. Static Cursors

    The complete result set of a static cursor is built in tempdb when the cursor is opened. A static cursor always displays the result set as it was when the cursor was opened. Static cursors detect few or no changes, but consume relatively few resources while scrolling. SQL Server static cursors are always read-only.

    3. Keyset Cursors

    The membership and order of rows in a keyset-driven cursor are fixed when the cursor is opened. Keyset-driven cursors are controlled by a set of unique identifiers, keys, known as the keyset. The keyset for a keyset-driven cursor is built in tempdb when the cursor is opened.

    4. Dynamic Cursors

    Dynamic cursors are the opposite of static cursors. Dynamic cursors reflect all changes made to the rows in their result set when scrolling through the cursor. The data values, order, and membership of the rows in the result set can change on each fetch. All UPDATE, INSERT, and DELETE statements made by all users are visible through the cursor.

    Examples Of Cursors

    The following cursor is defined for retrieving employee_id and employee_name from Employee table.The FETCH_STATUS value is 0 until there are rows. When all rows are fetched then FETCH_STATUS becomes 1.

                                 use DB_DNP  
                                SET NOCOUNT ON;    
      
                                DECLARE @emp_id int ,@emp_name varchar(20),    
                                @message varchar(max);    
      
                                PRINT '-------- EMPLOYEE DETAILS --------';    
      
                                DECLARE emp_cursor CURSOR FOR     
                                SELECT emp_id,emp_name    
                                FROM Employee  
                                order by emp_id;    
      
                                OPEN emp_cursor    
      
                                FETCH NEXT FROM emp_cursor     
                                INTO @emp_id, @emp_name    
      
                                print 'Employee_ID  Employee_Name'       
      
                                WHILE @FETCH_STATUS = 0    
                                BEGIN    
                                    print '   ' + CAST(@emp_id as varchar(10)) +'           '+  
                                        cast(@emp_name as varchar(20))  
      
          
                                    FETCH NEXT FROM emp_cursor     
                                    INTO @emp_id,@emp_name    
       
                                END     
                                CLOSE emp_cursor;    
                                DEALLOCATE emp_cursor; 
                            
    Trending Post
    What is Anonymous methods in C#.
    Why Is It A Bad Idea To Throw Your Own Exceptions.
    How's The DLL Hell Problem Solved In .net?
    Interview Questions and Answers
    Describe the Async method of C#?
    Is it possible to serialise hashtables?
    Explain the differences between “out” and “ref” parameters in C#?
    How would you implement the Singleton design pattern in C#?
    What Are The Ways To Deploy An Assembly?
    What Are Advantages And Disadvantages Of Microsoft-provided Data Provider Classes In Ado.net?
    About us

    DotNet Palace is a community platform created and maintained by The articles are mainly focused on Microsoft stack technologies like C#, ASP.Net, MVC, .Net Core, SQL Server and Oracle, PostgreSQL, SQLite etc. To improve the site's content you can send your valuable suggestions at info.dotnetpalace@gmail.com

    Quick links
  • SOLID Principles
  • Questions
  • OOPS Principles
  • DotNet Palace Tutorials
  • Privacy Policy
  • Terms and Condition