INSERT INTO #Temp (id, id2, a, b)
SELECT id, id2, a, b
FROM SomeOneTable
我们通常从一个表中获取数据,如果(id
和a
)字段来自单独的表(id2
和{,我们如何在临时表中插入数据? {1}})来自另一张桌子。我希望所有4个字段都在一个Temp表中。
答案 0 :(得分:5)
以INNER JOIN
为例
INSERT INTO #Temp (id, id2, a, b)
SELECT TABLE_A.ID, TABLE_B.ID2, TABLE_A.A, TABLE_B.B
FROM TABLE_A INNER JOIN TABLE_B ON TABLE_A.[primary_key] = TABLE_B.[foreign_key]
答案 1 :(得分:3)
试试这个:
INSERT INTO #Temp (id, id2, a, b)
SELECT id, id2, a, b
FROM SomeOneTable a inner join someonetable2 b on a.id=b.id2
答案 2 :(得分:0)
不使用内部联接: 假设您有两个表:Table1和Table2
INSERT INTO #TEMPTABLE (id, id2, a, b)
SELECT B.ID, C.ID2 , B.A, C.B FROM TABLE1 AS B , TABLE2 AS C