SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


s there any difference in using cursor and table?


s there any difference in using cursor and table?

Author
Message
sumith1andonly1
sumith1andonly1
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1220 Visits: 346
is there any difference in using cursor and table ?
any performance issues ?



sample are shown below

example 1:

declare rs_cursor CURSOR for select name from master.dbo.sysdatabases


example 2:

DECLARE @temp as table (RowID int not null primary key identity(1,1),
Filename varchar(500))

insert into @temp select name from master.dbo.sysdatabases
anthony.green
anthony.green
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62574 Visits: 8594
Yes there is typically a huge difference depending on what you want to do.

Cursors are typical RBAR meaning it processes a row at a time, where as with a table it can process a "set" of data at a time which is more effective.



How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


jad0083
jad0083
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 264
^ This, and they're usually not compared to each other, as they have totally different uses. What you mentioned is a table variable and is more aptly compared to a temp table, while cursors to while loops. You can use a cursor on a temp table, to do a per-row transaction/DML, but not vice versa. A good analogy would be like comparing a subject with an adjective.
sumith1andonly1
sumith1andonly1
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1220 Visits: 346
is it possible to fetch multiple columns (using cursor)
vinu512
vinu512
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8509 Visits: 1626
sumith1andonly1 (11/8/2012)
is it possible to fetch multiple columns (using cursor)


Yes it is possible to fetch multiple columns using cursors. You need to do a little research on Cursors.....the following link would help you a lot:

MSSQL Cursor

Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden ;-)
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)

Group: General Forum Members
Points: 145160 Visits: 13349
sumith1andonly1 (11/8/2012)
is it possible to fetch multiple columns (using cursor)


Let's be clear here:

cursors are sloooooooooooow.

Only use them if you have a very good reason to use them.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
vinu512
vinu512
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8509 Visits: 1626
Koen Verbeeck (11/8/2012)
sumith1andonly1 (11/8/2012)
is it possible to fetch multiple columns (using cursor)


Let's be clear here:

cursors are sloooooooooooow.

Only use them if you have a very good reason to use them.


Yes, I agree. Cursors are slow because they do row by row transactions instead of set based transactions which are much much much faster than row by row transactions.

Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden ;-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search