---
title: "Cursor in SQL"  
description: "In this blog, I’m explaining about cursor in SQL  A Cursor is a database object to retrieve data from a result set one row at a time. Life cycle of Cu"  
author: "priyanka kushwaha"  
published: 2015-01-28  
updated: 2015-01-28  
canonical: https://www.mindstick.com/blog/736/cursor-in-sql  
category: "mssql server"  
tags: ["sql"]  
reading_time: 1 minute  

---

# Cursor in SQL

In this blog, I’m explaining about [cursor in SQL](https://www.mindstick.com/interview/2594/what-is-cursor-in-sql-server)\

A Cursor is a [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) object to [retrieve data](https://www.mindstick.com/forum/160512/how-to-retrieve-data-from-the-database-in-c-sharp-using-dataadapter) from a result set one row at a time.

[Life cycle](https://www.mindstick.com/articles/23194/the-life-cycle-of-software-development) of Cursor

##### Declare cursor:

A cursor is declared by defining the SQL statement that returns a result set.

##### Open:

A cursor is opened and populated by executing the SQL statement defined by the cursor.

##### Fetch:

When cursor is opened , rows can be fetched from the cursor one by one or in a block to do [data manipulation](https://www.mindstick.com/forum/1919/image-manipulation-c-sharp).

##### Close:

After data manipulation , we should close the cursor explicitly.

##### Deallocate:

Finally, we need to delete the cursor [definition](https://yourviews.mindstick.com/view/70598/false-furore-over-the-leadership-definition-statement-of-army-chief-bipin-rawat) and released all the system [resources](https://yourviews.mindstick.com/view/293/renovation-causes-wastage-of-resources) associated with the cursor.

##### Example:

```
Create proc [dbo].[proCursor](@str nvarchar(50))

as

begin

declare @Name varchar(100)

declare @Id varchar(100)

declare cur cursor global static  for select Id,FirstName from EmployeeDetail where firstName=@str

open cur       fetch next from cur into @Id,@Name            while @@FETCH_STATUS=0                   begin

                             Print 'Id:'+@Id+' Name:'+@Name                              fetch next from cur into @Id,@Name              end       close cur  deallocate cur

End
```

---

Original Source: https://www.mindstick.com/blog/736/cursor-in-sql

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
