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


Please give me a replacement for a Cursor


Please give me a replacement for a Cursor

Author
Message
sunny-141957
sunny-141957
Old Hand
Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)

Group: General Forum Members
Points: 364 Visits: 36
Sub : I would like to have a replacement for a Cursor.

It should be other than the use of Table variables or
Temporary Tables.

Is there any other methods available.

regards
shown
Ninja's_RGR'us
Ninja's_RGR'us
SSC Guru
SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)

Group: General Forum Members
Points: 153958 Visits: 9672
Post the table definition, some sample data and the expected output from the query. We can't help you without that.
Jesper-244176
Jesper-244176
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3824 Visits: 33

If there is a unique id, then it is easy:

while (@id is not null)
select @id = min(id) from table where id > @id
and continue until you get null.


Ninja's_RGR'us
Ninja's_RGR'us
SSC Guru
SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)

Group: General Forum Members
Points: 153958 Visits: 9672
That's nice but how about we show him how to use the set based approach that he obviously doesn't know??
Jesper-244176
Jesper-244176
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3824 Visits: 33
Not sure I know that either, so I look forward to your reply...
Ninja's_RGR'us
Ninja's_RGR'us
SSC Guru
SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)

Group: General Forum Members
Points: 153958 Visits: 9672
Will have to look for his reply first .
sunny-141957
sunny-141957
Old Hand
Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)

Group: General Forum Members
Points: 364 Visits: 36
Mr Jesper,

I think this is the technique what U have meant.

****************************************************
eg:-

SELECT @id=MIN(id) FROM TABLE1
SET @minid=@id
WHILE @minid IS NOT NULL
BEGIN

------
-------
-------
SELECT @minid=MIN(id) FROM TABLE1 WHERE id > @minid
END

***********************************************************

If this is the technique if a table is very big that
doesn't help i feel. Every time U will have to check
the minimum of that table for manipulations.


Actually I know 3 techniques which are replacement for the
Cursors.

1) Using Temp tables
2) Using Table Variables
3) Finding the MIN value and manipulating row by row


I wanted some other technique which can be actually used on
big tables so that the manipulations does work faster.



regards
shown
Ninja's_RGR'us
Ninja's_RGR'us
SSC Guru
SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)

Group: General Forum Members
Points: 153958 Visits: 9672
Thanx Shown for this input, but I'd like to stress out that these methods are only LAST RESORTS, you must first try to find the SET BASED approach that will run faster 99.999% of the time compared to these work-arounds.
Jesper-244176
Jesper-244176
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3824 Visits: 33

You assumption is correct

I added a simple "select @minid" to the code above and constructed a similar cursor example. Then I ran the two algorithms on a table with 77000 rows. They both completed in 48 secs (you need an index on id, of course).

Are you looking for something even faster, or is there a different reason for not using cursors?


Ninja's_RGR'us
Ninja's_RGR'us
SSC Guru
SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)SSC Guru (153K reputation)

Group: General Forum Members
Points: 153958 Visits: 9672
Here's my thinking : Don't use cursors, ever. Then if you find a situation where a cursor is faster than a set based approach or there's no set based approach (some rare admin task), then go for it. But I assure you that it's not gonna happen often.
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