DEV Community

Clever Cottonmouth
Clever Cottonmouth

Posted on

What is the use of a cursor in SQL Server?

Cursors are a mechanism to explicitly enumerate through the rows of a result set, rather than retrieving it as such.

However, while they may be more comfortable to use for programmers accustomed to writing While Not RS.EOF Do ..., they are typically a thing to be avoided within SQL Server stored procedures if at all possible -- if you can write a query without the use of cursors, you give the optimizer a much better chance to find a fast way to implement it.

In all honesty, I've never found a realistic use case for a cursor that couldn't be avoided, with the exception of a few administrative tasks such as looping over all indexes in the catalog and rebuilding them. I suppose they might have some uses in report generation or mail merges, but it's probably more efficient to do the cursor-like work in an application that talks to the database, letting the database engine do what it does best -- set manipulation.

Image description

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (3)

Collapse
 
stevsharp profile image
Spyros Ponaris

Thanks for sharing.. To be honest, it has been many years since I last used cursors in SQL Server. Instead, I use @@ROWCOUNT to implement loops. Working with cursors can sometimes be dangerous and requires a lot of attention.
Cursors in SQL Server can be resource-intensive and are generally less efficient compared to set-based operations or alternatives like @@ROWCOUNT for looping.

Collapse
 
mirajhad profile image
Clever Cottonmouth

That makes sense! Cursors can be tricky and can impact performance if not handled properly. Using @@ROWCOUNT for loops is a great alternative in many cases. Recently, I used a cursor in my project to loop through data and store it in the database. In some scenarios, it felt like the best approach. Have you ever encountered a situation where a cursor was unavoidable?

Collapse
 
stevsharp profile image
Spyros Ponaris • Edited

That's a great question! To be honest, I don't use cursors anymore. Instead, I usually fetch the data into an in-memory table and use @@ROWCOUNT in a loop to process the rows.

-- Create temporary table
CREATE TABLE #ProductsTemp (ProductID INT PRIMARY KEY);

-- Insert sample data
INSERT INTO #ProductsTemp VALUES (1), (2), (3);

-- Declare variable
DECLARE @ProductID INT;

-- Loop through rows
WHILE EXISTS (SELECT 1 FROM #ProductsTemp)
BEGIN
-- Fetch the first ProductID
SELECT TOP 1 @ProductID = ProductID FROM #ProductsTemp;

DELETE FROM #ProductsTemp WHERE ProductID = @ProductID;
Enter fullscreen mode Exit fullscreen mode

END;

DROP TABLE #ProductsTemp;

Using this approach, I avoid the overhead of cursors while still maintaining row-by-row control.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more