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
 Merging three tables together into one Master tab

Author  Topic 

anjali66
Starting Member

23 Posts

Posted - 2011-01-07 : 13:57:07
Hello Everyone,

I have three tables. I need to merge these three tables into one table. There is an ID column in all these three tables. The ID's can be same in all three tables or they can be different. If the IDs are same then I need to merge based on those ID's so if the ID 1 exists in Table A, table B and Table C then one row will be created in the resulting master tables, but if the Id's are different then one individual row will be created in each table.

Also, some of the columns are same in all three tables, but some are different, but if the ID is same then I just want to append that column in the existing table.


I am not sure if I can do this. Please let me know if it is possible and if there is any faster solution for this.

Any help will be appreciated.

Thanks.



anjali66
Starting Member

23 Posts

Posted - 2011-01-07 : 14:03:22
I forgot to mention that I need all the columns in the resulting table.
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-01-07 : 14:13:01
SELECT *
FROM table1
UNION
Select *
FROM table2
UNION
SELECT *
FROM table3

if all structures are the same, a UNION will eliminate duplicates.



Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

anjali66
Starting Member

23 Posts

Posted - 2011-01-07 : 16:42:44
Thanks datagruru.
Go to Top of Page
   

- Advertisement -