Copy data into another table

SqlSql ServerSql Server-2008

Sql Problem Overview


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

Edit:

let's say there is a query

select * 
into table1 
from table2 
where 1=1 

which creates table1 with the same schema as well as data as in table2.

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

Sql Solutions


Solution 1 - Sql

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 newTable is optional if you are specifying a value for all columns and selecting columns in the same order as newTable's schema):

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

Solution 2 - Sql

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.

SELECT * INTO NewTable FROM OldTable

Solution 3 - Sql

This is the proper way to do it:

INSERT INTO destinationTable
SELECT * FROM sourceTable

Solution 4 - Sql

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

Solution 5 - Sql

Try this:

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

Solution 6 - Sql

Try this:

Insert Into table2
Select * from table1

Solution 7 - Sql

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;

Solution 8 - Sql

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

It is not mandatory column names to be same.

Solution 9 - Sql

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

the first query will create the structure from table1 to table2 and second query will put the data from table1 to table2

Solution 10 - Sql

Copy all columns from one table to another table:

INSERT INTO table2
SELECT * FROM table1
WHERE condition;

Copy only some columns from one table into another table:

INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;

You can duplicate or "clone" a table's contents by executing:

CREATE TABLE new_table AS SELECT * FROM original_table;

Solution 11 - Sql

-- for Sql Server users.

if you don't have the new table then you can create the new table with same structure as old table, and also copy data over from old table to the new table. For example:

select * into new_table
from old_table; 

also you can copy the column / table structure, and just some of data. For example:

select * into new_table
from old_table 
where country = 'DE'; 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRajaram ShelarView Question on Stackoverflow
Solution 1 - Sqllc.View Answer on Stackoverflow
Solution 2 - SqlSatish PatelView Answer on Stackoverflow
Solution 3 - SqlZzzView Answer on Stackoverflow
Solution 4 - Sqluser3566871View Answer on Stackoverflow
Solution 5 - SqlAbe MiesslerView Answer on Stackoverflow
Solution 6 - SqlKapil KhandelwalView Answer on Stackoverflow
Solution 7 - SqlNimmi VermaView Answer on Stackoverflow
Solution 8 - SqlS.AdikaramView Answer on Stackoverflow
Solution 9 - SqlRenish GotechaView Answer on Stackoverflow
Solution 10 - SqlAmandeepView Answer on Stackoverflow
Solution 11 - SqlMilad Hamid Elhaei SaharView Answer on Stackoverflow