Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Loop through table
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

10 Posts

Posted - 08/05/2011 :  17:04:05  Show Profile  Reply with Quote
How do i write a while loop that goes through an entire table?

Almighty SQL Goddess

38200 Posts

Posted - 08/05/2011 :  17:06:21  Show Profile  Visit tkizer's Homepage  Reply with Quote
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

Subscribe to my blog
Go to Top of Page

Most Valuable Yak

15732 Posts

Posted - 08/05/2011 :  17:16:25  Show Profile  Visit robvolk's Homepage  Reply with Quote
Probably for this:

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 - 08/06/2011 :  22:14:47  Show Profile  Reply with Quote
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:
Go to Top of Page

Esteemed SQL Purist

547 Posts

Posted - 08/07/2011 :  01:09:30  Show Profile  Visit jcelko's Homepage  Reply with Quote
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.

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


5072 Posts

Posted - 08/07/2011 :  11:09:24  Show Profile  Visit russell's Homepage  Reply with Quote
Ken, please ignore the rude outburst above.

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

Starting Member

10 Posts

Posted - 08/16/2011 :  12:10:46  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000