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)
 Merging two tables?

Author  Topic 

OldMySQLUser
Constraint Violating Yak Guru

301 Posts

Posted - 2008-02-08 : 08:25:15
I have two tables, each containing the same columns: one containing 1000 rows and one with 800 rows. How can I create a third table which is a merge of the other two and ensuring there are no duplicates please?

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-08 : 08:38:53

select columns from table1
union
select columns from table2


Madhivanan

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

sunil
Constraint Violating Yak Guru

282 Posts

Posted - 2008-02-08 : 08:39:19
Insert into table3
select * from table1
union
select * from table2
Go to Top of Page

OldMySQLUser
Constraint Violating Yak Guru

301 Posts

Posted - 2008-02-08 : 08:42:39
Thank you!
Go to Top of Page

OldMySQLUser
Constraint Violating Yak Guru

301 Posts

Posted - 2008-02-08 : 09:33:46
Would this also be correct, if I had more than one table?

example:

Insert into table3
select * from table1
union
select * from table2
select * from table4
select * from table5
Go to Top of Page

Qualis
Posting Yak Master

145 Posts

Posted - 2008-02-08 : 09:58:43
You need a union between each table.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-08 : 12:55:06
If you are sure you dont have same records repeated in more than one table (duplicates) you can use union all in place of union. it will be faster as it wont look for distinct value of records
Go to Top of Page
   

- Advertisement -