SQL 将数据复制到另一个表中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13237623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:57:19  来源:igfitidea点击:

Copy data into another table

sqlsql-serversql-server-2008

提问by Rajaram Shelar

How to copy/append data from one table into another table with same schema in SQL Server?

如何在 SQL Server 中将数据从一个表复制/追加到另一个具有相同架构的表中?

Edit:

编辑:

let's say there is a query

假设有一个查询

select * 
into table1 
from table2 
where 1=1 

which creates table1with the same schema as well as data as in table2.

table1使用与table2.

Is there any short query like this to only copy entire data only into an already existing table?

有没有像这样的简短查询只将整个数据复制到已经存在的表中?

回答by lc.

If both tables are truly the same schema:

如果两个表确实是相同的架构:

INSERT INTO newTable
SELECT * FROM oldTable

Otherwise, you'll have to specify the column names (the column list for newTableis optional if you are specifying a value for all columns and selecting columns in the same order as newTable's schema):

否则,您必须指定列名(newTable如果您为所有列指定一个值并以与newTable的架构相同的顺序选择列,则列列表是可选的):

INSERT INTO newTable (col1, col2, col3)
SELECT column1, column2, column3
FROM oldTable

回答by Zzz

This is the proper way to do it:

这是正确的方法:

INSERT INTO destinationTable
SELECT * FROM sourceTable

回答by Satish Patel

Simple way if new table does not exist and you want to make a copy of old table with everything then following works in SQL Server.

简单的方法,如果新表不存在,并且您想使用所有内容制作旧表的副本,然后在 SQL Server 中执行以下操作。

SELECT * INTO NewTable FROM OldTable

回答by user3566871

INSERT INTO table1 (col1, col2, col3)
SELECT column1, column2, column3
FROM table2                                        

回答by Abe Miessler

Try this:

尝试这个:

INSERT INTO MyTable1 (Col1, Col2, Col4)
   SELECT Col1, Col2, Col3 FROM MyTable2

回答by Kapil Khandelwal

Try this:

尝试这个:

Insert Into table2
Select * from table1

回答by S.Adikaram

INSERT INTO DestinationTable(SupplierName, Country)
SELECT SupplierName, Country FROM SourceTable;

It is not mandatory column names to be same.

相同的列名不是强制性的。

回答by Nimmi Verma

Insert Selected column with condition

插入带有条件的选定列

INSERT INTO where_to_insert (col_1,col_2) SELECT col1, col2 FROM from_table WHERE condition;

Copy all data from one table to another with the same column name.

将所有数据从一个表复制到另一个具有相同列名的表。

INSERT INTO where_to_insert 
SELECT * FROM from_table WHERE condition;

回答by Renish Gotecha

CREATE TABLE `table2` LIKE `table1`;
INSERT INTO `table2` SELECT * FROM `table1`;

the first query will create the structure from table1to table2and second query will put the data from table1to table2

第一个查询将创建结构从table1table2与第二查询将会把从数据table1table2