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
 General SQL Server Forums
 New to SQL Server Programming
 Joining Temp Tables

Author  Topic 

igor92128
Starting Member

23 Posts

Posted - 2011-12-15 : 20:22:30
Hello,

I have 2 temp tables that are pulling the same exact columns but have different values in those columns. I want to create a 3rd temp table with the combined values of the other 2 temp tables. Here is some snippets:

create table #table1
create table #table2
create table #combinedTables

insert #table1
select ...
from ...
where ...
--produces a 210 row table

insert #table2
select..
from...
where...
--produces a 100 row table

insert #combinedTables
select..
from...
where...
--should produce a 310 row temp table

Also, UNION doesn't work since it doesn't return the correct number of rows probably due to NULLs in the other tables.

Any ideas would be appreciated. I am on SQL 2005.

Thanks,
Igor

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-12-15 : 20:24:58
Use UNION ALL instead of UNION. Or, you could just do two inserts:
insert into #CombinedTable select * from #table1;
insert into #CombinedTable select * from #table2;
Go to Top of Page
   

- Advertisement -