• Jeff Moden (3/9/2013)


    Siobhan Perricone (3/8/2013)


    I got asked to not use a cursor for a loop that I'm working on so I grabbed the code here:

    http://www.sql-server-performance.com/2004/operations-no-cursors/%5B/quote%5D

    To emphasize what Gail has stated, they should outlaw articles like the one you provided the link for. Probably most important is the fact that the author has produced absolutely NO performance stats on the two methods and, like many of us had in the early days until we tested, has bought into the myth that Temp Tables or Table Variables and While loops are more efficient than Cursors. A well written "firehose" cursor is just as effecient and easier to code than such alternatives. It's usually a total waste of time to convert Cursors to alternatives. You won't actually believe it until you prove it to yourself with code.

    Don't buy into the "Cursor Replacement Myth" of replacing cursors with While Loops and Temp Tables. It usually makes no difference. If you feel you must, just convert the cursor to a "firehose" cursor and call it a day.

    What you should buy into, however, is that there's usually a very high performance method to avoid RBAR (see my sig line below for definition) altogether. Even multiple set-based passes on a Temp Table will blow any While loop away whether in a Cursor or not.

    I will third that opinion. This is an argument that I have heard time and again that a while loop is more efficient. Yuck.

    Here is a good article on comparing/ converting cursors to while loops and why the conversion just isn't what is advertised by that sql-server-performance article referenced above:

    http://sqlblog.com/blogs/aaron_bertrand/archive/2012/01/26/the-fallacy-that-a-while-loop-isn-t-a-cursor.aspx

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events