What is Cursor in SQL ? - GeeksforGeeks

19 Oct, 2021
Cursor is a Temporary Memory or Temporary Work Station. It is Allocated by Database Server at the Time of Performing DML(Data Manipulation Language) operations on Table by User. Cursors are used to store Database Tables. There are 2 types of Cursors: Implicit Cursors, and Explicit Cursors. These are explained as following below.
Implicit Cursors:Implicit Cursors are also known as Default Cursors of SQL SERVER. These Cursors are allocated by SQL SERVER when the user performs DML operations.Explicit Cursors :Explicit Cursors are Created by Users whenever the user requires them. Explicit Cursors are used for Fetching data from Table in Row-By-Row Manner.
Implicit Cursors:Implicit Cursors are also known as Default Cursors of SQL SERVER. These Cursors are allocated by SQL SERVER when the user performs DML operations.
Explicit Cursors :Explicit Cursors are Created by Users whenever the user requires them. Explicit Cursors are used for Fetching data from Table in Row-By-Row Manner.
How to create Explicit Cursor:
Declare Cursor Object.Syntax : DECLARE cursor_name CURSOR FOR SELECT * FROM table_nameDECLARE s1 CURSOR FOR SELECT * FROM studDetailsOpen Cursor Connection.Syntax : OPEN cursor_connectionOPEN s1Fetch Data from cursor.There are total 6 methods to access data from cursor. They are as follows :FIRST is used to fetch only the first row from cursor table.LAST is used to fetch only last row from cursor table.NEXT is used to fetch data in forward direction from cursor table.PRIOR is used to fetch data in backward direction from cursor table.ABSOLUTE n is used to fetch the exact nth row from cursor table.RELATIVE n is used to fetch the data in incremental way as well as decremental way.Syntax : FETCH NEXT/FIRST/LAST/PRIOR/ABSOLUTE n/RELATIVE n FROM cursor_nameFETCH FIRST FROM s1
FETCH LAST FROM s1
FETCH NEXT FROM s1
FETCH PRIOR FROM s1
FETCH ABSOLUTE 7 FROM s1
FETCH RELATIVE -2 FROM s1
Close cursor connection.Syntax : CLOSE cursor_nameCLOSE s1Deallocate cursor memory.Syntax : DEALLOCATE cursor_nameDEALLOCATE s1
Declare Cursor Object.Syntax : DECLARE cursor_name CURSOR FOR SELECT * FROM table_nameDECLARE s1 CURSOR FOR SELECT * FROM studDetails
DECLARE s1 CURSOR FOR SELECT * FROM studDetails
Open Cursor Connection.Syntax : OPEN cursor_connectionOPEN s1
OPEN s1
Fetch Data from cursor.There are total 6 methods to access data from cursor. They are as follows :FIRST is used to fetch only the first row from cursor table.LAST is used to fetch only last row from cursor table.NEXT is used to fetch data in forward direction from cursor table.PRIOR is used to fetch data in backward direction from cursor table.ABSOLUTE n is used to fetch the exact nth row from cursor table.RELATIVE n is used to fetch the data in incremental way as well as decremental way.Syntax : FETCH NEXT/FIRST/LAST/PRIOR/ABSOLUTE n/RELATIVE n FROM cursor_nameFETCH FIRST FROM s1
FETCH LAST FROM s1
FETCH NEXT FROM s1
FETCH PRIOR FROM s1
FETCH ABSOLUTE 7 FROM s1
FETCH RELATIVE -2 FROM s1

FETCH FIRST FROM s1
FETCH LAST FROM s1
FETCH NEXT FROM s1
FETCH PRIOR FROM s1
FETCH ABSOLUTE 7 FROM s1
FETCH RELATIVE -2 FROM s1

Close cursor connection.Syntax : CLOSE cursor_nameCLOSE s1
CLOSE s1
Deallocate cursor memory.Syntax : DEALLOCATE cursor_nameDEALLOCATE s1
DEALLOCATE s1
DBMS
SQL
DBMS
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between Clustered and Non-clustered index
CTE in SQL
SQL | Views
Third Normal Form (3NF)
Second Normal Form (2NF)
SQL | DDL, DQL, DML, DCL and TCL Commands
How to find Nth highest salary from a table
SQL | ALTER (RENAME)
SQL | Views
CTE in SQL