logo

SQL Server Table Variables To Eliminate Cursors

By Robbe Morris
Printer Friendly Version
View My Articles
142 Views
    

Here's a quick tip that allows you to duplicate the functionality of a cursor without using one in SQL Server 2000.


declare @RowCnt int
declare @MaxRows int
declare @Email nvarchar(255)
declare @rc int

select @RowCnt = 1

declare @Import table
(
rownum int IDENTITY (1, 1) Primary key NOT NULL ,
Email nvarchar(255)
)

insert into @Import (Email) values ('blah@blah.com')
insert into @Import (Email) values ('blahblah@blah.com')

select @MaxRows=count(*) from @Import

while @RowCnt <= @MaxRows
begin

select @rc=0

select @Email = Email
from @Import
where rownum = @RowCnt

print @Email

Select @RowCnt = @RowCnt + 1

end

 


Biography - Robbe Morris
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of EggHeadCafe.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.


Didn't Find The Answer You Were Looking For?

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Ask Question In Live Forum

If you have an OpenID and do not want to become a member of the EggHeadCafe forum, you can also sign on to Chat Chaos and post your question to our real time Silverlight chat application.
Ask Question In Chat Chaos

Article Discussion: SQL Server Table Variables To Eliminate Cursors
Robbe Morris posted at Sunday, September 17, 2006 7:58 PM
Original Article
 






  $1000 Contest    [)ia6l0 iii - $228  |  Jonathan VH - $161  |  Huggy Bear - $135  |  F Cali - $95  |  egg egg - $94  |  more Advertise  |  Privacy  |   (c) 2010