---
title: "CURSOR in SQL Server"  
description: "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 r"  
author: "AVADHESH PATEL"  
published: 2012-08-25  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/345/cursor-in-sql-server  
category: "database"  
tags: ["database"]  
reading_time: 2 minutes  

---

# CURSOR in SQL Server

A [Cursor](https://www.mindstick.com/articles/13003/create-a-simple-cursor-in-sql-server) is a [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) object that represents a [result](https://www.mindstick.com/blog/12011/advantages-of-getting-result-oriented-seo-from-an-agency) set and is used to [manipulate data](https://www.mindstick.com/forum/157877/how-do-you-use-filters-in-angularjs-to-manipulate-data) [row](https://www.mindstick.com/forum/1212/this-row-already-belongs-to-this-table) by row. When a cursor is opened, it is positioned on a row and that row is available for [processing](https://www.mindstick.com/blog/254/background-processing-in-android).\

For example, you can use a single [UPDATE](https://www.mindstick.com/forum/265/ajax-update-panel) statement to update many [rows of data](https://www.mindstick.com/forum/157166/what-is-the-best-and-fast-way-to-insert-2-million-rows-of-data-into-sql-server). There are times when you want to loop through a [series](https://answers.mindstick.com/qa/101432/what-s-the-highest-score-in-a-single-world-series-game) of rows a perform processing for each row. In this case you can use a cursor.

##### Syntax for Declaring Cursor

## ```
-- SyntaxDECLARE <CURSOR_NAME> CURSOR FOR SELECT <COLUMN_NAME> FROM <TABLE_NAME> WHERE <CONDITION>
```

##### Example for Declaring Cursor

```
-- ExampleDECLARE INFO_CURSOR CURSOR FOR SELECT * FROM INFO WHERE ID >
```

##### 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

```
--SyntaxOPEN <CURSOR_NAME>--ExampleOPEN 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

```
--SyntaxFETCH <CURSOR_NAME>--ExampleFETCH INFO_CURSOR
```

##### Example for Closing Cursor

```
--SyntaxCLOSE <CURSOR_NAME>--ExapleCLOSE 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_stdopen cur_std -- Open the cursorfetch next from cur_std -- Fetch next records in the cursor and display itfetch absolute 3 from cur_std  --Fetch 3 records in the cursorfetch prior from cur_std --Fetch previous record in the cursor
```

---

Original Source: https://www.mindstick.com/blog/345/cursor-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
