Explore Courses Blog Tutorials Interview Questions
0 votes
in SQL by (6.1k points)

How can I copy or append the data from one table into another with the same schema in the SQL Server?

I am having a query:

select * 
into table1 
from table2 
where 1=1 

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

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

1 Answer

0 votes
by (12.7k points)
edited by

If both the tables are exactly the same schema, use the following code:

SELECT * FROM oldTable

If the schema is not same, you will need to mention the column names (the column list for newTable is optional if you are defining a value for all columns and selecting columns in the similar order as newTable's schema):

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

Want to become a SQL Expert? Join this SQL training course and certification program.

Related questions

0 votes
1 answer
asked Jul 19, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories