---
title: "What is Cursor in SQL database? Write SQL command to create Cursor?"  
description: "What is Cursor in SQL database? Write SQL command to create Cursor?"  
author: "Ravi Vishwakarma"  
published: 2021-10-18  
updated: 2021-10-18  
canonical: https://www.mindstick.com/forum/156784/what-is-cursor-in-sql-database-write-sql-command-to-create-cursor  
category: "mssql server"  
tags: ["sql server"]  
reading_time: 3 minutes  

---

# What is Cursor in SQL database? Write SQL command to create Cursor?

[Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) about what is [Cursor in SQL](https://www.mindstick.com/interview/2594/what-is-cursor-in-sql-server) Server? Write a SQL cammand to create cursor in [SQL Server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server)

## Replies

### Reply by Ashutosh Kumar Verma

**[SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) [Cursor](https://www.mindstick.com/articles/13003/create-a-simple-cursor-in-sql-server) :**

Oracle creates a memory area, also called context area, to process SQL statements. Cursor is a pointer to this memory area and this memory (context) area is controlled by the cursor.

The size of the cursor is flexible according to its data. Oracle keeps some predefined space in the main memory to open the cursor, so the size of the cursor is limited. Whenever a query is run, the cursor works.

A Cursor is fetch all rows of a table one by one selected by the SQL statements. When I have need to check all rows of a table one by one or update data of that table then here we use Cursor.

## Types of Cursor:

There are two types of Cursor,

**1-** Implicit Cursor.

**2**- Explicit Cursor.

Implicit cursor: Whenever the SQL statement is executed, the implicit cursor is automatically created by Oracle (when there is no explicit cursor for the statement.)

'Programmers cannot control the implicit cursor and the information contained in it”. The cursor that is opened by Oracle for internal processing is called implicit cursor.

Explicit cursor: Explicit cursor is user-defined. Through Explicit cursor, we can have more control in the memory (context) area.

The cursor is used when some records from a table are accessed in a PL/SQL code block. SQL queries are used to declare this cursor.

Both these types of cursor have four common attributes, which are as follows:-

**%isopen:**- Returns true value if the cursor is open otherwise return false.

**%found:-** Returns true value if records are fetched successfully otherwise return false.

**%notfound:-** Returns true value if the records are not fetched successfully otherwise return false.

**%Rowcount:**- It returns the number of records processed.

## Create a Cursor:

```
DECLARE Variables;
DECLARE Cursor_Name CURSOR FOR
SQL Query
OPEN Cursor_Name
FETCH NEXT FROM Cursor_name INTO
VariableName
WHILE @@FETCH_STATUS=0
Process the Record
FETCH NEXT FROM Cursor_Name INTO VariableName
CLOSE Cursor_Name
DEALLOCATE Cursor_Name
```

Ex- Lets have take a database table ‘Student’

The following SQL statement is used cursor to retrieve the student ID and Name from above table.

![What is Cursor in SQL database? Write SQL command to create Cursor?](https://www.mindstick.com/mindstickforums/32ad9fa8-d27d-4354-bcac-014a2d5bc416/images/1713d161-1571-4671-b7a4-c04c3616756c.png)

![What is Cursor in SQL database? Write SQL command to create Cursor?](https://www.mindstick.com/mindstickforums/32ad9fa8-d27d-4354-bcac-014a2d5bc416/images/ee65fee0-6987-46fe-8625-4e615c0220e9.png)\

The following SQL statement is used to create a Cursor to fetch the distinct record from two table and show all records into a single form.

![What is Cursor in SQL database? Write SQL command to create Cursor?](https://www.mindstick.com/mindstickforums/32ad9fa8-d27d-4354-bcac-014a2d5bc416/images/8e0d5e17-d962-4238-ae36-12549467332e.png)

![What is Cursor in SQL database? Write SQL command to create Cursor?](https://www.mindstick.com/mindstickforums/32ad9fa8-d27d-4354-bcac-014a2d5bc416/images/f9c1358f-f7e5-48f1-9ea4-8e7b5cef3bd5.png)\

\


---

Original Source: https://www.mindstick.com/forum/156784/what-is-cursor-in-sql-database-write-sql-command-to-create-cursor

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
