0% found this document useful (0 votes)
7 views

Cursor in SQL Server

Sql

Uploaded by

Guru Sumanth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Cursor in SQL Server

Sql

Uploaded by

Guru Sumanth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Cursor in SQL Server

A cursor in SQL Server is a database object used to retrieve data row by row from a result
set. Cursors are useful when you need to perform operations on each row individually
rather than operating on the entire set at once, which is typical of SQL operations.
Here’s how a cursor works, explained with an example and dummy data.
Step-by-Step Example
1. Create Dummy Data Let’s assume we have a table named Employees with the
following data:
CREATE TABLE Employees (
EmployeeID INT,
EmployeeName VARCHAR(100),
Department VARCHAR(50),
Salary DECIMAL(10, 2)
);

INSERT INTO Employees (EmployeeID, EmployeeName, Department, Salary)


VALUES
(1, 'John Doe', 'HR', 5000.00),
(2, 'Jane Smith', 'Finance', 6000.00),
(3, 'Sam Green', 'IT', 5500.00),
(4, 'Alice Brown', 'HR', 5200.00);

Employees Table
Employees Table
Cursor Syntax
Let’s create a cursor that loops through the Employees table and increases the
salary of employees in the HR department by 10%. Here’s how we define and use a
cursor in SQL Server:

-- Declare variables to hold individual row data


DECLARE @EmployeeID INT, @EmployeeName VARCHAR(100), @Department VARCHAR(5
0), @Salary DECIMAL(10, 2);

-- Declare the cursor


DECLARE EmployeeCursor CURSOR FOR
SELECT EmployeeID, EmployeeName, Department, Salary
FROM Employees;

-- Open the cursor


OPEN EmployeeCursor;

-- Fetch the first row from the cursor


FETCH NEXT FROM EmployeeCursor INTO @EmployeeID, @EmployeeName, @Departmen
t, @Salary;
-- Start processing rows one by one
WHILE @@FETCH_STATUS = 0
BEGIN
-- If the employee is in the HR department, update the salary
IF @Department = 'HR'
BEGIN
-- Increase the salary by 10%
UPDATE Employees
SET Salary = Salary * 1.10
WHERE EmployeeID = @EmployeeID;

-- Print the updated info (for demonstration purposes)


PRINT 'Updated Salary for ' + @EmployeeName + ': ' + CAST(@Salary
* 1.10 AS VARCHAR(10));
END

-- Fetch the next row


FETCH NEXT FROM EmployeeCursor INTO @EmployeeID, @EmployeeName, @Depar
tment, @Salary;
END

-- Close and deallocate the cursor


CLOSE EmployeeCursor;
DEALLOCATE EmployeeCursor;

Cursor Flow Diagram

3. Explanation of Key Cursor Steps:


• Declare Cursor: The DECLARE EmployeeCursor statement defines the cursor
and selects the data you want to loop through.
• Open Cursor: The OPEN EmployeeCursor statement opens the cursor and
initializes it. •
Fetch Row: The FETCH NEXT statement retrieves the next row from the result set
and stores the column values in the specified variables.
• Process Rows: The WHILE @@FETCH_STATUS = 0 loop continues fetching rows
until there are no more rows to process (@@FETCH_STATUS is a system variable that
returns the status of the last fetch operation).
• Update Operation: Inside the loop, if the employee is in the “HR” department, the
cursor updates the employee’s salary by 10%.
• Close and Deallocate: After processing all the rows, the cursor is closed and
deallocated to free up memory.

Cursor Processing Rows


Cursor Processing Rows
4. Dummy Data Before Cursor Operation:
EmployeeID EmployeeName Department Salary
1 John Doe HR 5000.00
2 Jane Smith Finance 6000.00
3 Sam Green IT 5500.00
4 Alice Brown HR 5200.00
5. Final Output (After Cursor Operation):
EmployeeID EmployeeName Department Salary
1 John Doe HR 5500.00
2 Jane Smith Finance 6000.00
3 Sam Green IT 5500.00
4 Alice Brown HR 5720.00
Cursor Updating Salary
Cursor Updating Salary
Summary of the Output: • The salaries of John Doe and Alice Brown, both in the HR
department, have been increased by 10% as processed by the cursor.
When to Use Cursors: • When row-by-row processing is required. • When performing
complex row-level operations that can’t be done with standard SQL operations (like joins or
updates across rows).
However, keep in mind that cursors can be slow when processing large datasets and should
be used sparingly, favoring set-based SQL operations where possible.

You might also like