Blog
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog
Report Abuse Form
Reason:    
 

Home >> Database >> CURSOR in SQL Server
CURSOR in SQL Server

A Cursor is a database object that represents a result set and is used to manipulate data row by row. When a cursor is opened, it is positioned on a row and that row is available for processing.
Views: 423     Comments: 0
by AVADHESH PATEL on 8/25/2012

CURSOR in SQL Server

A Cursor is a database object that represents a result set and is used to manipulate data row by row. When a cursor is opened, it is positioned on a row and that row is available for processing.

For example, you can use a single UPDATE statement to update many rows of data. There are times when you want to loop through a series of rows a perform processing for each row. In this case you can use a cursor.

Syntax for Declaring Cursor

-- Syntax

DECLARE <CURSOR_NAME> CURSOR FOR SELECT <COLUMN_NAME> FROM <TABLE_NAME> WHERE <CONDITION>

Example for Declaring Cursor

-- Example

DECLARE INFO_CURSOR CURSOR FOR SELECT * FROM INFO WHERE ID >5

After declaring the cursor we can access the data. Before accessing the data of the cursor it must be opened by using open statement. Directly following a successful opening, the cursor is positioned before the first row in the result set.

Example for Opening Cursor

--Syntax

OPEN <CURSOR_NAME>

--Example

OPEN INFO_CURSOR

After opening cursor on a specific row in the result set with the fetch statement. A fetch operation transfers the data of the row into the application. Simply we can say by using fetch statement we read one by one row at a time in forward only by default.

Example for Fetching Cursor

--Syntax

FETCH <CURSOR_NAME>

--Example

FETCH INFO_CURSOR

Example for Closing Cursor

--Syntax

CLOSE <CURSOR_NAME>

--Exaple

CLOSE INFO_CURSOR

Example which demonstrate the use of Scrollable Cursor

This feature not available in SQL Server 2005. This is come from SQL Server 2008 and 2012

declare cur_std scroll cursor for select * from student --Declare a cursor named cur_std

open cur_std -- Open the cursor

fetch next from cur_std -- Fetch next records in the cursor and display it

fetch absolute 3 from cur_std  --Fetch 3 records in the cursor

fetch prior from cur_std --Fetch previous record in the cursor

 

Title :  
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
       
 
Report Abuse Form
Reason:    
 
Latest ArticleRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest Blogs by AVADHESH PATELRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 2645
  
Copyright © 2013MindStick. All Rights Reserved.