Search code examples
sql-servert-sqlwhile-loopdatabase-cursor

Cursor verus while loop - what are the advantages/disadvantages of cursors?


Is it a good idea to use while loop instead of a cursor?

What are the advantages/disadvantages of cursors?


Solution

  • Some of these depends on the DBMS, but generally:

    Pros:

    • Outperform loops when it comes to row-by-row processing

    • Works reasonably well with large datasets

    Cons:

    • Don't scale as well

    • Use more server resources

    • Increases load on tempdb

    • Can cause leaks if used incorrectly (eg. Open without corresponding Close)