Cursor is a database object which fetch data row by row basis. Cursor
is use for data manipulation and data processing purpose by fetching data by
performing loop. Cursor copy data to a temporary table for looping
purpose, the temporary table is automatically created on temdb database.Cursor
is a part of transaction SQL(T- SQL), here is the step to create a cursor
1)Declare cursor
2)Open cursor
3)Fetch record from cursor
4)Process your statement with the record
5)Fetch next record from the cursor
6)Close cursor
7)Deallocate cursor
Here is the syntax of a cursor
Declare cursor declared the cursor variable.
Select statement initialize the variable.Open cursor is opened for storing data retrieved from the result set.
Fetch statement fetch row from the cursor one by one.
Close release data and locking.
Deallocate destroy the cursor variable and release all the memory.
Here is example of a cursor
OUTPUT
John1 122018-01-08
John10 152018-10-08
John11 162018-11-08
John2 132018-02-08
John3 122018-03-08
John4 132018-04-08
John5 122018-05-08
John6 132018-06-08
John7 152018-07-08
John8 142018-08-08
John9 122018-09-08
cursor can be nested, cursor can be declared under another cursor. Below is the example of nested cursor.
Output:
You can specify some additional parameter during cursor declaration.
you should keep in mind that a cursor fetch data row by row basis, you can do row by row validation, computation, manipulation of data in each row. Cursor copy data to a temporary table for looping purpose, if this data is small, that is not an issue, but for the large data it is costly, put an extra burden on CPU is the cursor to manage cursor. That is why cursor is generally used for small record and forth large record, loop is used.
1)Declare cursor
2)Open cursor
3)Fetch record from cursor
4)Process your statement with the record
5)Fetch next record from the cursor
6)Close cursor
7)Deallocate cursor
Here is the syntax of a cursor
DECLARE cursor_name CURSOR [ LOCAL | GLOBAL ]
[ FORWARD_ONLY | SCROLL ]
[ STATIC | KEYSET | DYNAMIC | FAST_FORWARD ]
[ READ_ONLY | SCROLL_LOCKS | OPTIMISTIC ]
[ TYPE_WARNING ]
FOR select_statement
[ FOR UPDATE [ OF column_name [ ,...n ] ] ]
Declare cursor declared the cursor variable.
Select statement initialize the variable.Open cursor is opened for storing data retrieved from the result set.
Fetch statement fetch row from the cursor one by one.
Close release data and locking.
Deallocate destroy the cursor variable and release all the memory.
We
have created a table and corresponding data to do some sample
example.
CREATE
TABLE
student
(
id
INT
NOT
NULL,
name
VARCHAR
(500)
NOT
NULL,
age
INT
NOT
NULL,
adress
VARCHAR
(500)
NOT
NULL,
date_of_birth
DATETIME
NOT
NULL,
height
DECIMAL(10,2)
NOT
NULL
)
INSERT
INTO
student(id,name,age,adress,date_of_birth,height)
SELECT
1,'John1',12,'34,student
street','01/08/2018',10.5
UNION
SELECT
1,'John2',13,'35,student
street','02/08/2018',11
UNION
SELECT
1,'John3',12,'36,student
street','03/08/2018',11.5
UNION
SELECT
1,'John4',13,'37,student
street','04/08/2018',12
UNION
SELECT
1,'John5',12,'34,student
street','05/08/2018',12.5
UNION
SELECT
1,'John6',13,'35,student
street','06/08/2018',13.5
UNION
SELECT
1,'John7',15,'36,student
street','07/08/2018',14
UNION
SELECT
1,'John8',14,'34,student
street','08/08/2018',14.5
UNION
SELECT
1,'John9',12,'35,student
street','09/08/2018',15
UNION
SELECT
1,'John10',15,'36,student
street','10/08/2018',15.5
UNION
SELECT
1,'John11',16,'34,student
street','11/08/2018',16
Here is example of a cursor
DECLARE
db_cursor
CURSOR
FOR
SELECT
name,
age,
date_of_birth FROM
student
DECLARE
@Name VARCHAR(256);
DECLARE
@Age INT;
DECLARE
@dateOfBirth DATE;
OPEN
db_cursor;
FETCH
NEXT
FROM
db_cursor INTO
@Name,
@Age,
@dateOfBirth;
WHILE
@@FETCH_STATUS
=
0
BEGIN
--your
own statment
print
CONVERT(VARCHAR,@Name)+'
'+CONVERT(VARCHAR,@Age)+CONVERT(VARCHAR,@dateOfBirth)
FETCH
NEXT
FROM
db_cursor INTO
@Name,
@Age,
@dateOfBirth;
END;
CLOSE
db_cursor;
DEALLOCATE
db_cursor;
OUTPUT
John1 122018-01-08
John10 152018-10-08
John11 162018-11-08
John2 132018-02-08
John3 122018-03-08
John4 132018-04-08
John5 122018-05-08
John6 132018-06-08
John7 152018-07-08
John8 142018-08-08
John9 122018-09-08
cursor can be nested, cursor can be declared under another cursor. Below is the example of nested cursor.
DECLARE
db_cursor
CURSOR
FOR
SELECT
name,
age,
date_of_birth FROM
student
DECLARE
@Name VARCHAR(256);
DECLARE
@Age INT;
DECLARE
@dateOfBirth DATE;
DECLARE
@l_Name VARCHAR(256);
DECLARE
@l_Age INT;
DECLARE
@l_dateOfBirth DATE;
OPEN
db_cursor;
FETCH
NEXT
FROM
db_cursor INTO
@Name,
@Age,
@dateOfBirth;
WHILE
@@FETCH_STATUS
=
0
BEGIN
Print
'Age:'+CONVERT(VARCHAR,@Age)
------------------Nested
Cursor---------------------------------
DECLARE
db_nested
CURSOR
FOR
SELECT
name,
age,
date_of_birth
FROM
student2 where
age=@Age
OPEN
db_nested;
FETCH
NEXT
FROM
db_nested INTO
@l_Name,
@l_Age,
@l_dateOfBirth;
WHILE
@@FETCH_STATUS
=
0
BEGIN
Print
CONVERT(VARCHAR,@l_Name)+'
'+CONVERT(VARCHAR,@l_Age)+'
'+CONVERT(VARCHAR,@l_dateOfBirth)
FETCH
NEXT
FROM
db_nested INTO
@l_Name,
@l_Age,
@l_Age;
END;
CLOSE
db_nested;
DEALLOCATE
db_nested;
--------------------------------------------------------
FETCH
NEXT
FROM
db_cursor INTO
@Name,
@Age,
@dateOfBirth;
END;
CLOSE
db_cursor;
DEALLOCATE
db_cursor;
Age:12
Age:15
John4
15 2018-04-08
Age:16
John5
16 2018-05-08
Age:13
John2
13 2018-02-08
Age:12
Age:13
John2
13 2018-02-08
Age:12
Age:13
John2
13 2018-02-08
Age:15
John4
15 2018-04-08
Age:14
John3
14 2018-03-08
Age:12
You can specify some additional parameter during cursor declaration.
- Static Cursors :A static cursor populates the result set at the time of cursor creation and query result is cached for the lifetime of the cursor.
- Dynamic Cursors:A dynamic cursor allows you to see the data up dation, deletion and insertion in the data source while the cursor is open.
- Forward Only Cursors:A forward only cursor is the fastest cursor among the all cursors.Forward Only Cursors doesn't support backward scrolling.
you should keep in mind that a cursor fetch data row by row basis, you can do row by row validation, computation, manipulation of data in each row. Cursor copy data to a temporary table for looping purpose, if this data is small, that is not an issue, but for the large data it is costly, put an extra burden on CPU is the cursor to manage cursor. That is why cursor is generally used for small record and forth large record, loop is used.