[Webinar] Streamline your web hosting managementRegister Today


Contatinating 2 tables

Posted on 2008-02-05
Medium Priority
Last Modified: 2013-11-30
How do I concatenate two tables, identical in structure, into one table?

Question by:kpwhitte
LVL 40

Expert Comment

by:Richard Quadling
ID: 20828279
insert into table1 select * from table2

would do the trick initially.

but watch out for constraints on table1 which limit duplicate keys.
LVL 40

Accepted Solution

Richard Quadling earned 2000 total points
ID: 20828284
If you want to make a third table, ...

insert into table3 select * from table1 union all select * from table2

should work. You would need to create table3 first.
LVL 11

Expert Comment

ID: 20828288
Create a temp taable
Union the results from two tables and insert in temp table

LVL 50

Expert Comment

ID: 20828449
i don't (and i don't think  the others do either) understand what you mean by concatenate two tables together....,

do you want the whole set?    UNION ALL
do you want the unique set?  UNION
do you want to Update the rows with the same key using some business logic?

 which version of SQL Server are you using?

please explain in more detail what you are attempting to achieve and the key structure of the tables and the data (unique/non unique ...) dynamics involved..


Expert Comment

ID: 20830044
     Please do specify clearly what excatly is your requirement
 1. Do you wan your data to be combined and put in it a new table?
          insert into <New Table> select * from table1 union all select * from table2 -- replace the tagged portion with your new table name.
2.  Do you wish to insert all the records in Table2 into Table1?
     Insert into Table1 select * from Table2 -- If duplicate records are there in both the tables and you have primary key defined, then this will give you error.
3. Do you wish to create a third table and then insert all the values from Table1 and Table2 go with this.
    Create Table3 with the same structure as Table1 or Table2
    Insert into Table3 select * from Table1 union all select * from Table2

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question