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
 MERGE 3 TABLES

Author  Topic 

jfm
Posting Yak Master

145 Posts

Posted - 2012-05-18 : 08:27:51
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)

17689 Posts

Posted - 2012-05-18 : 08:35:06
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
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2012-05-18 : 08:44:58
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
[spoiler]Time is always against us[/spoiler]



Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-18 : 08:45:49
[code]
select <column list> from table1 union all
select <column list> from table2 union all
select <column list> from table3
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2012-05-18 : 08:54:16
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
[spoiler]Time is always against us[/spoiler]



Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-18 : 08:56:07
[code]
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
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2012-05-18 : 09:10:58

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
[spoiler]Time is always against us[/spoiler]



Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-18 : 09:13:37
you missed out the table alias O at the end


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2012-05-18 : 09:16:14
Its perfect!

Thank you very much.
Go to Top of Page
   

- Advertisement -