Home Forums SQL Server 7,2000 T-SQL Cursors - need to help to explain why to not use them RE: Cursors - need to help to explain why to not use them

  •  

    From my experience, use cursors only when there is no set based solution. And trust me, there are quite a few where there is just no set based solution (for example list files in each database from sysfiles, or users for that matter). If there is a set based solution, that beats the cursors hands down, every time I used. Even I used to use cursors until I have seen some very elegant solution from SQL gurus on this forum and other places that do not require cursors. One caveat is that when your using a set based solution  brings more data into memory and tempdb than the system capacity I may use cursors to stay within resources. Once again I see this as a system resource limit than the set based solution limit. One example is if you have three  very large tables (10 mil each ) a set based solution joins them and criteria requires about 1 million records from each table, your temp db should hold 1 mil * 1 mil * 1 mil rows. If you do not have that space, you may limit the query to go againt N records each time using the cursor depending on the memory and tempdb space.