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?

3

11 Answers

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 
8

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 

This is the proper way to do it:

INSERT INTO destinationTable SELECT * FROM sourceTable 
0
INSERT INTO table1 (col1, col2, col3) SELECT column1, column2, column3 FROM table2 

Try this:

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

Try this:

Insert Into table2 Select * from table1 

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; 
INSERT INTO DestinationTable(SupplierName, Country) SELECT SupplierName, Country FROM SourceTable; 

It is not mandatory column names to be same.

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

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; 

-- 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';