SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 MERGE 3 TABLES
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jfm
Posting Yak Master

134 Posts

Posted - 05/18/2012 :  08:27:51  Show Profile  Reply with Quote
Hi there,

I need to merge, 3 tables.

I have 3 tables in the same database, but in order to work with my data (as given in my 3 tables) I need to join the 3 tables in a only new one.

Take table A, B,C and merge all them creating table D. The number of columns of each table is the same, and also the values

Any idea?

Thanks

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 05/18/2012 :  08:35:06  Show Profile  Reply with Quote
quote:
The number of columns of each table is the same, and also the values

you mean the 3 tables are identical ?

How do you want to merge these 3 tables ?


KH
Time is always against us

Go to Top of Page

jfm
Posting Yak Master

134 Posts

Posted - 05/18/2012 :  08:44:58  Show Profile  Reply with Quote
The tree tables, have identical column names. The info inside each cell is different (they are numbers), but the data style is the same for each same column in each table.

Is the same as if you have all your info in tree sheets and you want to put the info in only one sheet.




quote:
Originally posted by khtan

quote:
The number of columns of each table is the same, and also the values

you mean the 3 tables are identical ?

How do you want to merge these 3 tables ?


KH
Time is always against us



Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 05/18/2012 :  08:45:49  Show Profile  Reply with Quote

select <column list> from table1 union all 
select <column list> from table2 union all
select <column list> from table3



KH
Time is always against us

Go to Top of Page

jfm
Posting Yak Master

134 Posts

Posted - 05/18/2012 :  08:54:16  Show Profile  Reply with Quote
Perfect,

But i want also to put the tree tables into a new_table

I have tried:

select <column list> from table1 union all
select <column list> from table2 union all
select <column list> from table3 union all
into Total_File

msg 156, level 15, state 1, line 4




quote:
Originally posted by khtan


select <column list> from table1 union all 
select <column list> from table2 union all
select <column list> from table3



KH
Time is always against us



Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 05/18/2012 :  08:56:07  Show Profile  Reply with Quote

SELECT <column list>
INTO   NEW_TABLE
FROM
(
    select <column list> from table1 union all 
    select <column list> from table2 union all
    select <column list> from table3
) O



KH
Time is always against us

Go to Top of Page

jfm
Posting Yak Master

134 Posts

Posted - 05/18/2012 :  09:10:58  Show Profile  Reply with Quote

SELECT * INTO NEW_TABLE
FROM

(SELECT * FROM TABLE_A UNION ALL
SELECT * FROM TABLE_B UNION ALL
SELECT * FROM TABLE_C )


Msg 102, level 15, state 1, line 5
incorrect syntax near ')'


I did the query using all the columns name, and I have the same msg..

Any idea?

Thanks a lot



quote:
Originally posted by khtan


SELECT <column list>
INTO   NEW_TABLE
FROM
(
    select <column list> from table1 union all 
    select <column list> from table2 union all
    select <column list> from table3
) O



KH
Time is always against us



Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 05/18/2012 :  09:13:37  Show Profile  Reply with Quote
you missed out the table alias O at the end


KH
Time is always against us

Go to Top of Page

jfm
Posting Yak Master

134 Posts

Posted - 05/18/2012 :  09:16:14  Show Profile  Reply with Quote
Its perfect!

Thank you very much.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000