SQL Server › Cursors

How would you rewrite a common cursor based row by row update into an equivalent, much faster set based UPDATE statement?

Advanced

Answer

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.

Common follow-ups: What situations genuinely require row by row processing that cannot be converted to a set based operation?;How do you measure the actual performance difference between a cursor and its set based equivalent?