Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

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

Edit:

I mean to say there is 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?

1 Answer

0 votes
by (40.7k points)

If both tables are truly the same schema:

INSERT INTO newTable

SELECT * FROM oldTable

Otherwise, you need to specify the column names:

(If you are specifying the value for all columns and selecting columns in the same order as newTable's schema, then the column list for newTable will be optional ):

INSERT INTO newTable (col1, col2, col3)

SELECT column1, column2, column3

FROM oldTable

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...