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
 3 TABLES MERGE INTO A NEW ONE

Author  Topic 

jfm
Posting Yak Master

145 Posts

Posted - 2012-05-17 : 04:46:37
Hi there,

I have 3 tables in my database1.

I need to copy this 3 tables in 1. The columns of each table have the same name, what I need is instead of having 3 tables for the same kind of info, just create a new one with all that info.

Thanks

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-05-17 : 07:50:50


Try this

create table #table1 (Yourcol int)

insert into #table1(Yourcol)
select '120'
union all
select '102'


create table #table2 (Yourcol int)

insert into #table2 (Yourcol)
select '150'
union all
select '162'

select * into #table3 from (
select Yourcol from #table1
union all
select Yourcol from #table2)T


select * from #table3





If you want unique records from both of your table then instead of
using UNION ALL use UNION

Vijay is here to learn something from you guys.
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2012-05-18 : 04:26:12
Hi there,

What is the value for "(Yourcol int)" is that the number of columns that i do have for each of my tables?

Thanks

create table #table1 (Yourcol int)

insert into #table1(Yourcol)
select '120'
union all
select '102'


create table #table2 (Yourcol int)

insert into #table2 (Yourcol)
select '150'
union all
select '162'

select * into #table3 from (
select Yourcol from #table1
union all
select Yourcol from #table2)T


select * from #table3



[/code]

If you want unique records from both of your table then instead of
using UNION ALL use UNION

Vijay is here to learn something from you guys.
[/quote]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-19 : 14:46:59
you want them to be merged inside same column or in different column?

if former use Vijays query


insert into #table2 (id,col)
select id,column
from table1

union

select id,column
from table2

union

select id,column
from table3



if you want to merge them horizontally keeping them in different columns use

insert table (id,columntb1,columntbl2,columntbl3)
select t1.id,t1.column,t2.column,t3.column
from table1 t1
inner join table2 t2
on t2.id = t1.id
inner join table3 t3
on t3.id = t1.id


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2012-05-21 : 05:54:42
Thank you very much!!

select id,column
from table2

union

select id,column
from table3



if you want to merge them horizontally keeping them in different columns use

insert table (id,columntb1,columntbl2,columntbl3)
select t1.id,t1.column,t2.column,t3.column
from table1 t1
inner join table2 t2
on t2.id = t1.id
inner join table3 t3
on t3.id = t1.id
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/


[/quote]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-21 : 16:02:53
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -