---
title: "create a simple cursor in sql server"  
description: "In this article we are going to explain what is Cursor in sql or how to create Cursor in sql or how to use Cursor in sql server with example."  
author: "Anonymous User"  
published: 2018-07-11  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/13003/create-a-simple-cursor-in-sql-server  
category: "mssql server"  
tags: ["sql server", "sql"]  
reading_time: 2 minutes  

---

# create a simple cursor in sql server

## [Introduction](https://www.mindstick.com/blog/359/iphone-development-introduction):

In this article, we are going to explain what is **[Cursor in SQL](https://www.mindstick.com/forum/156784/what-is-cursor-in-sql-database-write-sql-command-to-create-cursor)** or how to create Cursor in SQL or how to use Cursor in SQL server with an example.

## Description:

**A Cursor** is a SQL Object, or we can say like a [Virtual table](https://www.mindstick.com/interview/876/which-virtual-table-does-a-trigger-use) that retrieves data from the table one row at a time. We use **cursors** when we need to update or [delete records](https://answers.mindstick.com/qa/104793/how-to-delete-records-in-oracle) in a [database](https://www.mindstick.com/forum/160888/how-much-does-it-cost-to-get-training-in-database-in-mindstick-training) row by row. You can evaluate data row-by-row. Think of cursors as a stored data set that then lets you go through each row, manipulate or view a field, and then perform some kind of logic against the record based on a field's value. In most cases, you don't need a **cursor** in [your code](https://answers.mindstick.com/qa/35617/how-do-you-make-sure-that-your-code-is-both-safe-and-fast), but it's good to understand cursor syntax and how it [functions](https://www.mindstick.com/forum/161928/explain-the-python-functions-with-explanation) differently from other **SQL** code.

## Let’s see simply how to create a cursor

```
Declare @ID as bigintDeclare @Name as varchar(120)Declare @Address as varchar(200)Declare MY_CURSOR CURSOR FORSelect ID ,Name,Address1 from CUSTOMER (Nolock)OPEN MY_CURSOR  PRINT ' Customers '   PRINT '=============================='     FETCH NEXT FROM MY_CURSOR INTO @ID ,@Name,@Address        WHILE @@FETCH_STATUS = 0        BEGIN   PRINT 'ID :'+CAST(@ID AS VARCHAR(15))   PRINT 'ID :'+CAST(@ID AS VARCHAR(15))   PRINT 'Name :'+ @NAME  PRINT 'Address :'+@Address  PRINT'______________________'        FETCH NEXT FROM MY_CURSOR INTO @ID ,@Name,@Address        END    CLOSE MY_CURSORDEALLOCATE MY_CURSOR
```

In above example, we have created a cursor that fetch customer record row by row and print formatted design which shown below:

## OutPut:

![create a simple cursor in sql server](https://www.mindstick.com/mindstickarticle/94fbc152-4854-4af0-9407-3a4b2787059b/images/b6ae47d5-80ca-49b8-bec2-a00d615414f8.png)**\**

I hope it will help you after reading it.

You might also like to read [Multi-statement Table-Valued User-Defined Function in SQL Server](https://www.mindstick.com/Articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server), [table valued User-Defined Function in SQL Server](https://www.mindstick.com/Articles/12999/what-is-table-valued-function-in-sql-server), [scalar User-Defined Function in SQL Server](https://www.mindstick.com/Articles/12998/what-is-scalar-function-in-sql)

\

---

Original Source: https://www.mindstick.com/articles/13003/create-a-simple-cursor-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
