Cursor in SQL
Cursor in SQL
Cursor in SQL
The cursor in SQL is the same as the looping technique of other programming
languages. The collection of tuples held by the cursor is known as the active set.
1. Implicit Cursor
2. Explicit Cursor
Implicit Cursor
These types of cursors are generated and allocated by the SQL server when the system
performs INSERT, DELETE, and UPDATE operations on SQL queries.
An implicit cursor is also created by the system when the SELECT query selects the single
row.
Explicit Cursor
These types of cursors are created by the user using the SELECT query.
An explicit cursor holds multiple records but processes a single row at a time. It uses the
pointer, which moves to another row after reading one row.
It is basically used for gaining extra control over the temporary workstation.
Syntax:
DECLARE @YourVariables nvarchar(50) //You have to declare all the require
d variables
DECLARE My_Cursor_Name CURSOR // You have to declare the Name of y
our Cursor
[LOCAL | GLOBAL] // You have to specify the Scope of yo
ur Cursor
[FORWARD_ONLY | SCROLL] // You have to specify the movement dir
ection of your Cursor
[ KEYSET | DYNAMIC |STATIC | FAST_FORWARD] // You have to specify the B
asic type of your Cursor
[ SCROLL_LOCKS | OPTIMISTIC |READ_ONLY ] // You have to specify the Lock
s for your Cursor
OPEN My_Cursor_Name // You have to Open Your Cursor
FETCH NEXT FROM My_Cursor_Name // This line fetches the data from your
Cursor
CLOSE My_Cursor_Name // Here, you have to close Your Cursor
DEALLOCATE My_Cursor_Name // Here, you have to deallocate the cursor m
emory.
CREATE TABLE Student
(
Student_RollNo INT PRIMARY KEY,
Student_Name nvarchar(60) NOT NULL,
Student_Course nvarchar(20) NOT NULL,
Student_Age INTNOT NULL,
Student_Marks INT NOT NULL
) ;
INSERT INTO Student (Student_RollNo, Student_Name, Student_Cours
e, Student_Age, Student_Marks) VALUES ( 1, Amit, BCA, 19, 88),
( 2, Rahul, MCA, 21, 98),
( 3, Jones, B.tech, 20, 93),
( 4, Riya, BCA, 19, 89),
( 5, Aaniya, BBA, 21, 92),
( 6, Saket, MCA, 19, 95),
( 7, Shobhit, MBA, 20, 90),
( 8, Ishika, BCA, 21, 89),
( 9, Parul, B.tech, 19, 91),
( 10, Yukti, BCA, 20, 96);
DECLARE @Student_RollNo INT, @Student_Name NVARCHAR(50), @Student
_Course NVARCHAR(50) /*Here, we declare the variables for holding data.
*/
/* Here, we declare and set counter */
DECLARE @Counter INT
SET @Counter = 1
PRINT '-------- Record of Students --------';
/* Declare the cursor*/
DECLARE Print_Student_Details CURSOR
FOR
SELECT Student_RollNo, Student_Name, Student_Course FROM customer
/* Open the cursor */
OPEN Print_Student_Details
/* Fetch the record from the cursor into the variables. */
FETCH NEXT FROM Print_Student_Details INTO
@Student_RollNo, @Student_Name, @Student_Course
/* LOOP UNTIL RECORDS ARE AVAILABLE. */
WHILE @@FETCH_STATUS = 0
BEGIN
IF @Counter = 1
BEGIN
PRINT 'Student_RollNo' + CHAR(9) + 'Student_Name' + CHAR(9) + CHAR(9)
+ 'Student_Course'
PRINT '--------------------------'
END
/* This statement prints the current record */
PRINT CAST(@ Student_RollNo AS NVARCHAR(10)) + CHAR(9) + @Student_N
ame + CHAR(9) + CHAR(9) + @Student_Course
/* This statement increments the counter variable */
SET @Counter = @Counter + 1
/* This statament fetch the next record into the variables. */
FETCH NEXT FROM Print_Student_Details INTO
@Student_RollNo, @Student_Name, @Student_Course
END
/* This statement closes the cursor*/
CLOSE Print_Student_Details
/* This statement deallocates the cursor*/
DEALLOCATE Print_Student_Details