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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Best way to loop through multiple records

Author  Topic 

sallymac
Starting Member

3 Posts

Posted - 2008-10-13 : 17:12:34
Hello,
I have about 20,000 records in my db i want to loop through, process one by one by a function and then log.

My question is whats the best way to loop through.


[Code]
declare @loop int, @csv varchar(max)

set @loop = @@rowcount
while @loop =1
begin


update top 1000 mytable set updatestatus = 1 where updatestatus = 0

set @csv = select top 1000

Select @csv = isnull(@csv + ', ', '') + products
From myTable
Where updatestatus = 1

---pass @csv to billing function then log
[/Code]

end

thanks

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2008-10-13 : 19:13:05
What is special about your processing that you need to do it in a function? Ideally, you don't loop through a table processing each record; you apply the processing to the set of records in the table.

=======================================
It is impossible to imagine Goethe or Beethoven being good at billiards or golf. -H.L. Mencken, writer, editor, and critic (1880-1956)
Go to Top of Page
   

- Advertisement -