单个sql插入查询

时间:2011-07-07 05:28:06

标签: sql sql-server sql-server-2008

表A
专栏

1.Int 
2.Name          
3.Int1
4.Name1

表B. 专栏

1.Int  
2.Name

如何在单个查询中将表A的所有列插入表B列。?

4 个答案:

答案 0 :(得分:4)

如果我理解你需要的是:insert select command

INSERT INTO  b (col1,col2)
SELECT col1,col2 FROM a 
UNION ALL
SELECT col3,col4 FROM a 

答案 1 :(得分:1)

INSERT INTO B (Int, Name)
SELECT
  CASE x.f WHEN 0 THEN A.Int  ELSE A.Int1  END,
  CASE x.f WHEN 0 THEN A.Name ELSE A.Name1 END
FROM A
  CROSS JOIN (SELECT 0 AS f UNION ALL SELECT 1) x

该方法基本上描述为here

答案 2 :(得分:0)

INSERT INTO  table1 (field1, field2) SELECT field1, field2 FROM table2

答案 3 :(得分:0)

--insert the all columns record from table A to table B

INSERT INTO  tableB ([Int], [Name]) 
SELECT [Int], [Name] FROM tableA
UNION ALL
SELECT [Int1], [Name1] FROM tableA