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
 insert new column that from other table

Author  Topic 

sweet_monic
Starting Member

3 Posts

Posted - 2012-09-27 : 04:06:57
example i have 3 tables

table1
A B C
a1 b1 c1
a2 b2 c2
a3 b3 c3

table2
C D E
c1 d1 e1
c2 d2 e2
c3 d3 e3

table3
E F G
e1 f1 g1
e2 f2 g2
e3 f3 g3

the output i want is
table1
A B C G
a1 b1 c1 g1
a2 b2 c2 g2
a3 b3 c3 g3

the select for G i got it

select t3.G from table3 t3 join table2 t2 on t3.E = t2.E
join table3 t3 on t2.C=t3.C

from this select i got

G
g1
g2
g3

so how i insert this data to table1 so the output is just like that?

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2012-09-27 : 04:18:30
[code]
select t1.a
,t1.b
,t1.c
,t3.g
from table1 as t1
inner join table2 as t2
on t2.c=t1.c
inner join table3 as t3
on t3.e=t2.e
[/code]
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2012-09-27 : 04:21:24
Try this

select t1.A,t1.B,t1.C,t3.G
from table1 t1
inner join table2 t2 on t1.C =t2.C
inner join table3 t3 on t2.E = t3.E

Senthil Kumar C
------------------------------------------------------
MCITP - Database Administration SQL SERVER 2008
MCTS - Database Development SQL SERVER 2008
Go to Top of Page

sweet_monic
Starting Member

3 Posts

Posted - 2012-09-27 : 04:29:38
i want to insert to the existing table1

i already alter table1 by add new field G can i do this?

insert into [dbo].[table1] (G)
select t3.G
from table3 t3 join table2 t2 on t3.E = t2.E
join table3 t3 on t2.C = t3.C
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2012-09-27 : 04:35:56
Then You have to go for Update with join

Update t1 set t1.E=t3.E
from table1 t1
inner join table2 t2 on t1.C =t2.C
inner join table3 t3 on t2.E = t3.E



Senthil Kumar C
------------------------------------------------------
MCITP - Database Administration SQL SERVER 2008
MCTS - Database Development SQL SERVER 2008
Go to Top of Page

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2012-09-27 : 04:37:54
[code]
update table1
set g=table3.g
from table1
inner join table2
on table2.c=table1.c
inner join table3
on table3.e=table2.e
[/code]
Go to Top of Page

sweet_monic
Starting Member

3 Posts

Posted - 2012-09-27 : 04:46:35
aaahhhh..... ic..ic...thank u all
Go to Top of Page
   

- Advertisement -