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)
 Results of Union into Temp Table

Author  Topic 

MJ1210
Starting Member

3 Posts

Posted - 2008-04-03 : 16:10:16
This may be a dumb question, but I can't seem to get the syntax right. I have two temp tables that have the same columns, I want to do a union on them and store the results in a temp table. Any ideas?

Ie.

select * from #tmpTable1
union
select * from #tmpTable2
into #tmpTable3

Thanks!!!

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-04-03 : 16:12:59
create # table(col.....)
go

insert into #table
select * from #tmpTable1
union
select * from #tmpTable2
Go to Top of Page

MJ1210
Starting Member

3 Posts

Posted - 2008-04-03 : 16:20:56
Thanks! So this will put it in a local temp table (ie. the temp table will be deleted when stored procedure is done executing?)

Thanks so much!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-04-04 : 02:17:14
or

Select * into #tmpTable3 from
(
select * from #tmpTable1
union
select * from #tmpTable2
) as t

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-04-04 : 02:18:43
quote:
Originally posted by MJ1210

Thanks! So this will put it in a local temp table (ie. the temp table will be deleted when stored procedure is done executing?)

Thanks so much!


If you create a temp table in a procedure, it would be dropped automatically once it is executed

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -