SQL Server: Select from two tables and insert into one

故事扮演 提交于 2019-12-04 19:39:20

As both table variables have a single row you can cross join them.

INSERT INTO MyTable
            (col1,
             col2,
             col3,
             col4)
SELECT t.colA,
       t.colB,
       t.colC,
       t2.colD
FROM   @temp t
       CROSS JOIN @temp2 t2 

you should use this if you have only single row in both table @temp and @temp2, because this is a cartesian product.

INSERT INTO MyTable(col1,col2,col3,col4)
  SELECT t.colA,
           t.colB,
           t.colC,
           t2.colD
    FROM   @temp t,@temp2 t2
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!