Link to home
Start Free TrialLog in
Avatar of kpwhitte
kpwhitte

asked on

Contatinating 2 tables

How do I concatenate two tables, identical in structure, into one table?

thanks.
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

insert into table1 select * from table2

would do the trick initially.

but watch out for constraints on table1 which limit duplicate keys.
ASKER CERTIFIED SOLUTION
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vs1784
vs1784

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

Thanks,
Avatar of Lowfatspread
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..

Hi,
     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