想要了解有关通过加入插入的信息

时间:2014-11-10 05:46:58

标签: sql-server sql-server-2008 tsql sql-server-2005 sql-server-2012

我知道如何使用join更新表格,如:

update t1
    set t1.id=t2.id2
from #table1 t1
inner join #table2 t2
    on t1.id=t2.id2 

但我不知道插入。 如何从#table1(id,name)插入数据 使用table2 (id,name)到#join

3 个答案:

答案 0 :(得分:1)

INSERT INTO table2(id, name)
SELECT t1.id, t1.name FROM table1 t1
 INNER JOIN table2 t2
 ON t1.id = t2.id;

答案 1 :(得分:1)

insert into  #table2(id,name) 

select  #table1 t1.id,t1.name

from #table1 t1

        inner join #table2 t2

        on t1.id=t2.id2 

答案 2 :(得分:0)

可能是你看起来像这样:

insert into  #table2 (id,name) 
select  #table1 t1.id,t1.name
from #table1 t1
        inner join #table2 t2
        on t1.id=t2.id2