# temp tables (not global) are available in the area they created below. So you could do something like ...
while (your_condition = 1) begin
set @sql = 'select ... into #temp1 ...from blah
exec sp_do_the_inserts'
exec(@sql)
end
sp_do_the_inserts might look like ...
select * into #temp2 from #temp1
....your special logic here....
This assumes that you create sp_do_the_inserts in advance, of course. I don't know if this serves you.
joshp source
share