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
 insert new column that from other table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sweet_monic
Starting Member

Indonesia
3 Posts

Posted - 09/27/2012 :  04:06:57  Show Profile  Reply with Quote
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
Constraint Violating Yak Guru

277 Posts

Posted - 09/27/2012 :  04:18:30  Show Profile  Reply with Quote

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

Edited by - bitsmed on 09/27/2012 04:18:48
Go to Top of Page

senthil_nagore
Flowing Fount of Yak Knowledge

India
1007 Posts

Posted - 09/27/2012 :  04:21:24  Show Profile  Visit senthil_nagore's Homepage  Send senthil_nagore a Yahoo! Message  Reply with Quote
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

Indonesia
3 Posts

Posted - 09/27/2012 :  04:29:38  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
1007 Posts

Posted - 09/27/2012 :  04:35:56  Show Profile  Visit senthil_nagore's Homepage  Send senthil_nagore a Yahoo! Message  Reply with Quote
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
Constraint Violating Yak Guru

277 Posts

Posted - 09/27/2012 :  04:37:54  Show Profile  Reply with Quote

update table1
   set g=table3.g
 from table1
      inner join table2
              on table2.c=table1.c
      inner join table3
              on table3.e=table2.e
Go to Top of Page

sweet_monic
Starting Member

Indonesia
3 Posts

Posted - 09/27/2012 :  04:46:35  Show Profile  Reply with Quote
aaahhhh..... ic..ic...thank u all
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