In this article we will learn about cursor and types of cursors in detail, with examples.
Introduction
Cursor:cursor is a memory location for storing database tables. Cursor is a temporary work area allotted to the client at the server when a select stint is executed. A cursor contains information on a select stint and the rows of data accessed by it. This temporary work area is used to store the data retrieved from the database and manipulate this data.
A cursor can hold more than one row but can process only one row at a time The set of rows the cursor holds is called the result set.
Types of cursor
- Implicit cursor
These cursors will be created by SQL Server by default when select stint executes.
- Explicit Cursor
When user creates a memory location to store the tables then it is called Explicit cursor
Note
Whenever we want to do record by record manipulation the explicit cursor will be used.
Steps to create Explicit Cursor
- Declare a cursor
In this process we define a cursor
Syntax - Declare <cursor name> cursor for <Select stint>
- Open a cursor
When we open a cursor it will internally execute the select stint that is associated with the cursor declaration and load the data into the cursor.
Syntax - Open <Cursor name>
- Fetching Data from the cursor
In this process we access row by row from cursor
Syntax - Fetch first/last/next/prior /absolute n/relative n from <cursor name> [into <variables>]
- Closing a cursor
In this process it releases the cursor result set of the cursor leaving the data structure available for reporting.
Syntax - close <cursor name>
- Deallocate a cursor
It removes the cursor reference and deallocates it by destroying the data structure.
Syntax - Deallocate <cursor name>
@@Fetch_status
It is a global variable use to check whether cursor variable contains records or not. If the record is there the value will be zero, and otherwise the value will be -1
Fetching Methods in Cursor
- Next -It is a default method of cursor which can be used for fetching the records in forward directional only
- First - Fetching first record from the table
- Last - Fetching Last record from the table.
- Prior - Fetching the current record to previous record (backward directional)
- Absolute n - Fetching exact position of the record from the Table.n is the number of records.
- Relative n - Fetching the records either in incremental or decremental order.
Note
The cursors are again classified into 2 types,
- Forward-only cursor
- Scroll cursor
If a cursor is declared as forward only, it allows you to navigate only to the next records in sequential order, and moreover it supports only a singleton fashion that is fetched next (in a one-by-one way)
Whereas a scroll cursor allows you to Navigate /Fetch bi-directionally; that is, top-bottom or bottom-top, and it supports six different fetch methods:
- Fetch Next
- Fetch First
- Fetch Last
- Fetch Prior
- Fetch absolute n
- Fetch relative n
Example
Write a cursor to update Employee Salary as per given criteria.
For each department name, the salary increases in the given percentage:
Department Name | Increment |
Technology | 5 |
Sales | 7 |
Hr | 3 |
Create table Empdetails and insert some values
- create table Empdetails(id int,dname varchar(50),salary int)
-
- insert into Empdetails values(1,'Technology',2000)
- insert into Empdetails values(2,'Sales',25000)
- insert into Empdetails values(3,'Hr',12000)
Now we create a cursor.
- declare @id int,@dname varchar(50)
- declare cur cursor for select id,dname from Empdetails
- open cur
- fetch next from cur into @id,@dname
- while @@FETCH_STATUS=0
- begin
- if @dname='Development'
- update Empdetails set salary=salary*5 where id=@id
- else if @dname='sales'
- update Empdetails set salary=salary*7 where id=@id
- else
- update Empdetails set salary =salary*3 where id=@id
- fetch next from cur into @id,@dname
- end
- close cur
- deallocate cur
Check the table,
Example
Write a cursor to fetch records from last to first:
- declare c1 cursor scroll for select * from company
- open c1
- fetch last from c1
- while @@FETCH_STATUS=0
- begin
- fetch prior from c1
- end
- close c1
- deallocate c1
Result
Summary
In this article we learned about cursor in T/SQL. Cursor is a memory location for storing database tables.