SQL Server › Cursors
How would you rewrite a common cursor based row by row update into an equivalent, much faster set based UPDATE statement?
AdvancedAnswer
You identify the specific logic being applied to each row inside the cursor's loop, and translate that same logic directly into a single UPDATE statement with an appropriate WHERE clause or JOIN, letting SQL Server's query engine apply the change to every matching row at once instead of looping through them individually.
-- Cursor based approach (slow)
-- Loops through each product, updating price one at a time
-- Set based equivalent (fast)
UPDATE Products
SET Price = Price * 1.1
WHERE Category = 'Electronics';Real-world example A team replaces a slow cursor that increased electronics prices by ten percent one row at a time with a single set based UPDATE statement, cutting the operation's runtime from several minutes down to under a second.