SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

latent_ken
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?

tkizer
Almighty SQL Goddess

USA
37142 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
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

robvolk
Most Valuable Yak

USA
15676 Posts

Posted - 08/05/2011 :  17:16:25  Show Profile  Visit robvolk's Homepage  Reply with Quote
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 - 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:
http://www.sqloptimizationsschool.com/Pages/Advanced%20Concepts/Loops%20vs.%20Cursors.aspx
Go to Top of Page

jcelko
Esteemed SQL Purist

USA
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.

--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

USA
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

latent_ken
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  
 New 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.08 seconds. Powered By: Snitz Forums 2000