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)
 How to iterate through a temp table?

Author  Topic 

dundealing
Starting Member

12 Posts

Posted - 2007-10-25 : 12:03:18
Hi,

I am trying to iterate through the rows in a temporary table and get the rowcount from a different table based on a certain value?

For example, I have a temp table with 5 columns. I have populated 4 columns with the values from table A. I now need to fill the final column with the count(*) from table B where an ID is the same as that in the temp table's first column.

It goes something like this:

Create table #temp
(
A char,
B char,
C char,
D char
)

insert #temp (A,B,C,D)
select A, B, C, D from dbo.Alphabet

I now need to insert into #temp the count of all rows in table dbo.Words where WordsID = whatever is in the A column, and I need to do this for every row in #temp.

I tried a while loop but it threw up some syntax errors.

Any help would be much appreciated.

nr
SQLTeam MVY

12543 Posts

Posted - 2007-10-25 : 12:11:37
update #temp
set cnt = (select count(*) from Words where #temp.A = Words.WordsID)




==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-10-25 : 14:13:51
When's the assignment due?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

dundealing
Starting Member

12 Posts

Posted - 2007-10-28 : 09:06:35
Many thanks,nr, that looks good to me.

X002548, it isn't an assignment, I am just new to SQL.
Go to Top of Page
   

- Advertisement -