While loop or cursor

Hello, all

Sometime i surf the internet and got confused because today 

i search for while loop and Cursor difference and mechanism / performance 

but i got different answers in diffrent pages some says while is fast and some says cursor is faster in most conditions.

Can anyone please clear me with the stuff??

Must appreciated for the knowledge s

February 3rd, 2015 2:47am

Hi Dilip,

in my opinion (and not only mine) the best way is "Set Base Thinking"

take a look to this post

Free Windows Admin Tool Kit Click here and download it now
February 3rd, 2015 3:06am

Same performance, try to avoid using cursors and loops in T-SQL.
February 3rd, 2015 3:09am

You may refer the below article. Always try to avoid ROW based operations and go for SET BASED operation.

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

Free Windows Admin Tool Kit Click here and download it now
February 3rd, 2015 3:17am

You need to avoid cursors as much as possible as its impacts perfomance.

While loop is good to use.

If your requirement doest fullfill by using while loop you can go for cursors.But need to avoid cursors as much as u can.

Thanks,

Rohit 

February 3rd, 2015 3:41am

Try to use set base approach only and if you got something which can be implemented using loop only then use While loop instead of cursor. 

Thanks,

Saurabh 

Free Windows Admin Tool Kit Click here and download it now
February 3rd, 2015 3:55am

Typically one uses WHILE with cursor. Generally no difference.

Example for cursor WHILE loop:  http://www.sqlusa.com/bestpractices2008/rebuild-all-indexes/

As noted above, first choice is set-based programming since cursor solutions may not scale well.

February 3rd, 2015 4:20am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics