Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I loop through each row in a table variable

Posted on 2009-04-14
8
Medium Priority
?
1,078 Views
Last Modified: 2012-05-06
For MS SQL 2005. I have a table variable with no ID column. How do I loop through each row, or how do I create an ID column so I can loop through it?
0
Comment
Question by:NobleRLC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 24141659
What do you hope to accomplish by looping through the table?
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 2000 total points
ID: 24141684
I'm with unified in that you probably don't need to loop.  But here is how you define an ID column in your table variable.

declare @SomeTable table
  (ident       int identity primary key clustered
  ,field1       ......
....
)
0
 
LVL 22

Expert Comment

by:dportas
ID: 24141685
The simple answer is: Don't. It's better to avoid looping through rows one at a time by writing set-based queries instead. Also, every table should have a key. There's no excuse for not having one on a table variable.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:NobleRLC
ID: 24141832
I'm passing variables from the table variable to a stored procedure executed on each row.
0
 
LVL 22

Expert Comment

by:dportas
ID: 24142012
You could pass all the rows to the proc as a table-valued parameter.
Or you could write the proc so that it works on the whole result instead of one row at a time.

Try to avoid writing procs that need to be called for every single row. Doing it that way greatly increases the complexity of your code and destroys performance and scalability.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24142134
Table valued parameters aren't available until 2008. :)  But the rest is all valid about doing procs to work with data sets not rows.
0
 
LVL 27

Expert Comment

by:Chris Luttrell
ID: 24142149
When you have to do something like that we avoid cursors and do something like the code below, assuming that you have to leave the original table alone and that your values are unique so the delete just deletes the one:

Select Variable 
into #TmpTable  -- could be table variable, but you have to set it up
from Table
 
DECLARE @thisVariable [typeneeded]
WHILE EXISTS (SELECT * FROM #tmpTable)
BEGIN
	SELECT TOP 1 @thisVariable = Variable FROM #TmpTable
	
	EXEC yourStoredProcedure (@thisVariable)
	
	DELETE FROM #TmpTable WHERE Variable = @thisVariable
END
DROP TABLE #TmpTable

Open in new window

0
 
LVL 22

Expert Comment

by:dportas
ID: 24142275
If you must call a proc for each row then I suggest you use an explicit DECLARE CURSOR iwith the FAST_FORWARD option. Using a cursor has the advantage over SELECT TOP because you can specify the order without having to requery the whole table on each iteration of the loop. Also a DECLARE CURSOR arguably makes it more explicit what is happening in loop.
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question