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
"select * into table" Will it work for inserting data into existing table
提问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 * into
query.
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
You should try
你应该试试
INSERT INTO ExistingTable (Columns,..)
SELECT Columns,...
FROM OtherTable
Have a look at INSERT
看看插入
and SQL SERVER – Insert Data From One Table to Another Table – INSERT INTO SELECT – SELECT INTO TABLE
和SQL SERVER – 将数据从一个表插入到另一个表 – INSERT INTO SELECT – SELECT INTO TABLE
回答by Aaronaught
No, you cannot use SELECT INTO
to 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 INTO
in 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 adolf garlic
回答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