“select * into table” Will it work for inserting data into existing table

风流意气都作罢 提交于 2019-12-20 12:27:55

问题


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

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.

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


回答1:


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




回答2:


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

The documentation makes this very clear:

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

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.




回答3:


Update from CTE? http://www.sqlservercentral.com/Forums/Topic629743-338-1.aspx




回答4:


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

INSERT INTO yourtable2 SELECT * FROM yourtable1



来源:https://stackoverflow.com/questions/2688281/select-into-table-will-it-work-for-inserting-data-into-existing-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!