在SQL中将数据从Table1插入Table2

时间:2016-04-20 10:00:02

标签: sql-server sql-insert

我有两个表,table1有2列,table2有3列。

我想将table1 col1 and table1 col2的值插入到一列的table2中。

以下是table1的数据:

╔════════╦════════╗
║  Col1  ║  Col2  ║
╠════════╬════════╣
║ Value1 ║ Value2 ║
╚════════╩════════╝

将其插入table2行:例如

╔════════╦
║ Value1 ║
╠════════╣
║ Value2 ║
╚════════╝

如何使用SQL执行此操作?

1 个答案:

答案 0 :(得分:0)

如果要将每个复制在不同的行中,请使用:

INSERT INTO table2 (col1, col2)
SELECT col1, col2 FROM table1;

或逐一

INSERT INTO table2 (col1)
SELECT col1 FROM table1;

INSERT INTO table2 (col2)
SELECT col2 FROM table1; 

如果要在同一行中连接,请使用

INSERT INTO table2 (col3)
    SELECT col1 + col2 FROM table1;

OR

INSERT INTO table2 (col3)
   SELECT col1 || col2 FROM table1;

http://www.w3schools.com/sql/sql_insert_into_select.asp