Answer
Cursors are database objects that allow row-by-row traversal of a result set, similar to iterators in procedural languages.
Reasons they are discouraged:
- They process data one row at a time, leading to poor performance on large sets.
- They often hold locks for a long time, reducing concurrency.
- They introduce complex, procedural logic that is harder to maintain and test.
- They typically have higher memory and tempdb overhead compared to set-based alternatives.
Cursors should be a last resort, used only when set-based solutions are impractical or impossible. In many cases, window functions, MERGE statements, or carefully written set-based updates can replace cursor logic.