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
 Loop through table

Author  Topic 

latent_ken
Starting Member

10 Posts

Posted - 2011-08-05 : 17:04:05
How do i write a while loop that goes through an entire table?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-08-05 : 17:06:21
Just use a cursor for that. Details including example can be found in Books Online.

HOWEVER! Why do you need to do this? There is almost certainly a better way to handle your problem than looping through all rows in a table. Please provide some details as to why you think you need to loop through every row.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-08-05 : 17:16:25
Probably for this: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=163968

You definitely don't need (or want) a cursor if you're asking what I think you're asking.
Go to Top of Page

Jason W
Starting Member

19 Posts

Posted - 2011-08-06 : 22:14:47
Here is how you can loop through a table, although there is probably a good chance that you can write your query without any looping at all. But here you go:
http://www.sqloptimizationsschool.com/Pages/Advanced%20Concepts/Loops%20vs.%20Cursors.aspx
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-08-07 : 01:09:30
The short answer is "use a cursor"

The RIGHT answer is NEVER use a cursor. SQL words with entire sets of data at one time, not like mag tapes and record at time processing. You need to study RDBMS for about a year before you post again.

--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-08-07 : 11:09:24
Ken, please ignore the rude outburst above.

Of course feel free to post when you have questions.
Go to Top of Page

latent_ken
Starting Member

10 Posts

Posted - 2011-08-16 : 12:10:46
Thank you for all the help. I am trying to get it done without looping. The other thread listed above is related to this one so i will ask questions there.
Go to Top of Page
   

- Advertisement -