19

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 ?

Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286

4 Answers4

27

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

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
  • 1
    Can you do this by selecting all columns using *? `INSERT INTO ExistingTable (*) SELECT * FROM OtherTable` – Ryan Chase Dec 15 '15 at 16:33
4

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.

Aaronaught
  • 120,909
  • 25
  • 266
  • 342
1

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

INSERT INTO yourtable2 SELECT * FROM yourtable1

ShruS
  • 73
  • 7
  • This does not provide an answer to the question. You can [search for similar questions](//stackoverflow.com/search), or refer to the related and linked questions on the right-hand side of the page to find an answer. If you have a related but different question, [ask a new question](//stackoverflow.com/questions/ask), and include a link to this one to help provide context. See: [Ask questions, get answers, no distractions](//stackoverflow.com/tour) – Bugs Nov 09 '17 at 10:26
0

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

adolf garlic
  • 3,034
  • 7
  • 39
  • 54