Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 cursor vs table variable

Author  Topic 

laddu
Constraint Violating Yak Guru

332 Posts

Posted - 2009-06-24 : 00:53:47
Can anyone explain the difference? when to use them? Thanks!

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-24 : 01:33:52
A cursor is (in my words) a record set and functionality / abitlity to process the rows from that record set one by one.
Cursors are very slow! In most cases you don't need cursors if you are thinking set based.

A table variable is a variable that can be used like an ordinary table. You can find many examples in this forum for using table variables to provide sample data or to return a table (variable) from a function.

Hope that helps.
Webfred


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-24 : 01:36:46
when to use them?

cursor : don't use them. Bad for performance
table variable : OK


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Directory
Starting Member

5 Posts

Posted - 2009-06-24 : 01:38:02
those topic will helpful to you

http://www.lazydba.com/sql/1__13921.html

http://www.sqlteam.com/article/cursor-performance

Conclusion: cursor can do processing one row at a time.
table variable can process look like temp table.
It can process multiple rows.

:)
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-24 : 02:04:37
Table variables can be bad when populated with more than one page of data.
Since there is no statistics for table variables, sql engine always assumes there is one record, and one record only, in the table variables which can lead to bad query plans.


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-24 : 02:40:42
Why cursor gives bad performance.
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=128082

from 60 mins to 60 secs


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -