Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

temp table traversal in stored procedure

Status
Not open for further replies.

paraglidersd

Programmer
May 21, 2008
7
US
m pretty inexperienced when it comes to sql, but I am an experienced engineer.

Want to create a stored procedure. Within that procedure I am going to create a temp table with certain attributes read in from 2 other permanent tables. After that, within the stored procedure, I want to read the resulting rows of the temp table one by one (one of the attributes will be a key that I will use to access yet another permanent table). I dont know how to do this.

select key1, att1 into #temp_table
from permTable1
where
key > xx
date > 999999

insert #temp_table
select key1, att1 from permTable1History
where
key > xx
date > 999999

....this is where I am stuck....how do I now read the contents of the #temp_table within the stored procedure one by one, to pull out that key attribute, to use to read from another permanent table?

would it be
select * from temp_table
(select id from permTable3 where exists)......blah blah...?

thanks,
Bill
 
Writing loops with or without a cursor is slow and you can almost always avoid it.
You probably don't need the temp table either

e.g.
select ..
from t1
join t3
on t1.key=t3.key
union all
select ..
from t2
join t3
on t2.key=t3.key

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top