0% found this document useful (0 votes)
4 views3 pages

Cursor

In SQL Server, a cursor is a database object that allows for row-by-row data retrieval and manipulation, useful for procedural data processing. There are two types of cursors: implicit cursors, which are automatically created by the database engine for single-row operations, and explicit cursors, which are defined by the programmer for more complex operations. The process of using a cursor involves declaring, opening, fetching, processing, closing, and optionally deallocating the cursor.

Uploaded by

sujallakhani2716
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Cursor

In SQL Server, a cursor is a database object that allows for row-by-row data retrieval and manipulation, useful for procedural data processing. There are two types of cursors: implicit cursors, which are automatically created by the database engine for single-row operations, and explicit cursors, which are defined by the programmer for more complex operations. The process of using a cursor involves declaring, opening, fetching, processing, closing, and optionally deallocating the cursor.

Uploaded by

sujallakhani2716
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Explain cursor in detail.

▪ In SQL Server, a cursor is a database object used to retrieve and manipulate data row
by row, rather than fetching the entire result set at once.
▪ Cursors are primarily used in situations where set-based operations are not feasible or
efficient, such as when processing data in a procedural manner or when performing
row-level operations based on specific conditions.
How a cursor works in SQL Server:
▪ Declare Cursor: You declare a cursor by specifying a SELECT statement that defines
the result set to be processed.
▪ Open Cursor: After declaring the cursor, you open it to establish the result set.
▪ Fetch Cursor: Once the cursor is open, you fetch rows from the result set one at a time,
usually in a loop. Each fetch operation retrieves the next row from the result set.
▪ Process Data: As you fetch each row, you can perform operations or calculations based
on the data in that row.
▪ Close Cursor: After you have finished processing all the rows, you close the cursor to
release the resources associated with it.
▪ Deallocate Cursor: Optionally, you can deallocate the cursor to remove it from
memory entirely.
Types of Cursor:
There are two main types: implicit cursors and explicit cursors.

Implicit Cursors: Implicit cursors are automatically created by the SQL Server database
engine whenever a SELECT, INSERT, UPDATE, or DELETE statement is executed.
These cursors are managed internally by the database engine, and you don't have direct control
over them. Implicit cursors are handy for single-row operations where you don't need to process
multiple rows explicitly.
Example:
Select * From Employee
--Cursor Start from Here
DECLARE @FirstName VARCHAR(50)
DECLARE @LastName VARCHAR(50)
DECLARE @Email VARCHAR(100)
DECLARE @Phone VARCHAR(20)
DECLARE @Department VARCHAR(50)
DECLARE @Position VARCHAR(50)
DECLARE @Salary DECIMAL(10, 2)

DECLARE EmployeeCursor CURSOR FOR


SELECT FirstName, LastName, Email, Phone, Department, Position, Salary
FROM Employee

OPEN EmployeeCursor

FETCH NEXT FROM EmployeeCursor


INTO @FirstName, @LastName, @Email, @Phone, @Department, @Position, @Salary
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'Name: ' + @FirstName + ' ' + @LastName + ', Email: ' + @Email + ', Phone: ' +
@Phone + ', Department: ' + @Department + ', Position: ' + @Position + ', Salary: ' +
CONVERT(VARCHAR(20), @Salary)
FETCH NEXT FROM EmployeeCursor INTO @FirstName, @LastName, @Email,
@Phone, @Department, @Position, @Salary
END
CLOSE EmployeeCursor
DEALLOCATE EmployeeCursor
Explicit Cursors: Explicit cursors are defined explicitly by the programmer using the
DECLARE CURSOR statement. With explicit cursors, you have more control over the cursor
behavior, such as defining the result set, specifying cursor options, and explicitly fetching rows.
Explicit cursors are suitable for scenarios where you need to process multiple rows sequentially
or perform more complex cursor operations.

DECLARE @ID INT


DECLARE @FirstName VARCHAR(50)
DECLARE @LastName VARCHAR(50)
DECLARE @Class VARCHAR(50)
DECLARE @Grade VARCHAR(10)
DECLARE @Email VARCHAR(100)
DECLARE @Mobile VARCHAR(20)
DECLARE @City VARCHAR(50)

DECLARE StudentCursor CURSOR FOR


SELECT ID, FirstName, LastName, Class, Grade, Email, Mobile, City
FROM Student

OPEN StudentCursor

FETCH NEXT FROM StudentCursor INTO @ID, @FirstName, @LastName, @Class, @Grade, @Email,
@Mobile, @City
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'ID: ' + CAST(@ID AS VARCHAR(10)) + ', Name: ' + @FirstName + ' ' +
@LastName + ', Class: ' + @Class + ', Grade: ' + @Grade + ', Email: ' + @Email + ',
Mobile: ' + @Mobile + ', City: ' + @City
FETCH NEXT FROM StudentCursor INTO @ID, @FirstName, @LastName, @Class, @Grade,
@Email, @Mobile, @City
END

CLOSE StudentCursor
DEALLOCATE StudentCursor

You might also like