SQL “select * into table”是否可以将数据插入现有表中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2688281/
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 06:03:24  来源:igfitidea点击:

"select * into table" Will it work for inserting data into existing table

sqlsql-serverdatabasesql-server-2005tsql

提问by Shantanu Gupta

I am trying to insert data from one of my existing table into another existing table.

我正在尝试将数据从我现有的一个表中插入到另一个现有表中。

Is it possible to insert data into any existing table using select * intoquery. I think it can be done using union but in that case i need to record all data of my existing table into temporary table, then drop that table and finally than apply union to insert all records into same table

是否可以使用select * into查询将数据插入任何现有表中。我认为可以使用 union 来完成,但在这种情况下,我需要将现有表的所有数据记录到临时表中,然后删除该表,最后应用 union 将所有记录插入到同一个表中

eg.

例如。

select * into #tblExisting from tblExisting
drop table tblExisting
select * into tblExisting from #tblExisting union tblActualData

Here tblExisting is the table where I actually want to store all data tblActualData is the table from where data is to be appended to tblExisting.

这里 tblExisting 是我实际想要存储所有数据的表 tblActualData 是将数据附加到 tblExisting 的表。

Is it right method. Do we have some other alternative ?

方法对不对。我们还有其他选择吗?

回答by Adriaan Stander

回答by Aaronaught

No, you cannot use SELECT INTOto insert data into an existing table.

不,您不能使用SELECT INTO将数据插入到现有表中。

The documentationmakes this very clear:

文档使这个非常清楚:

SELECT…INTO creates a new tablein the default filegroup and inserts the resulting rows from the query into it.

SELECT...INTO在默认文件组中创建一个新表,并将查询的结果行插入其中。

You generally want to avoid using SELECT INTOin production because it gives you very little control over how the table is created, and can lead to all sorts of nasty locking and other performance problems. You should create schemas explicitly and use INSERT- even for temporary tables.

您通常希望避免SELECT INTO在生产中使用,因为它几乎无法控制表的创建方式,并且可能导致各种令人讨厌的锁定和其他性能问题。您应该显式创建模式并使用INSERT- 即使是临时表。

回答by ShruS

@Ryan Chase Can you do this by selecting all columns using *?Yes!

@Ryan Chase 你能通过使用 * 选择所有列来做到这一点吗?是的!

INSERT INTO yourtable2 SELECT * FROM yourtable1

INSERT INTO yourtable2 SELECT * FROM yourtable1