Home Forums SQL Server 2008 T-SQL (SS2K8) Query Help- Cursor change into FOR WHILE LOOP, TEMP TABLE RE: Query Help- Cursor change into FOR WHILE LOOP, TEMP TABLE

  • manub22 (7/26/2012)


    The reason for the WHILE LOOP is because with CURSOR it is much slower than with WHILE LOOP.

    Who said CURSOR is slower than WHILE loops? Didn't you asked them reason for the same?

    While working with cursors, there are lot of options you can set to make them more performant, check the comments above by @jeff.

    Also check this blog post where I've shown CURSORS giving good performance than WHILE LOOPS: http://sqlwithmanoj.wordpress.com/2011/02/07/avoid-cursors-or-use-them-optimally/[/url]

    Interesting blog post Manoj. It is true you can make cursors more optimal than a while loop but why bother with them at all? In your examples there is absolutely nothing that can't be done far quicker by getting rid of the loop entirely.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/