SQL Server - Create a copy of a database table and place it in the same database?
Solution 1
Use SELECT ... INTO
:
SELECT *
INTO ABC_1
FROM ABC;
This will create a new table ABC_1
that has the same column structure as ABC
and contains the same data. Constraints (e.g. keys, default values), however, are -not- copied.
You can run this query multiple times with a different table name each time.
If you don't need to copy the data, only to create a new empty table with the same column structure, add a WHERE
clause with a falsy expression:
SELECT *
INTO ABC_1
FROM ABC
WHERE 1 <> 1;
Solution 2
Copy Schema (Generate DDL) through SSMS UI
In SSMS expand your database in Object Explorer, go to Tables, right click on the table you're interested in and select Script Table As, Create To, New Query Editor Window.
Do a find and replace (CTRL + H) to change the table name (i.e. put ABC
in the Find What field and ABC_1
in the Replace With then click OK).
Copy Schema through T-SQL
The other answers showing how to do this by SQL also work well, but the difference with this method is you'll also get any indexes, constraints and triggers.
Copy Data
If you want to include data, after creating this table run the below script to copy all data from ABC (keeping the same ID values if you have an identity field):
set identity_insert ABC_1 on
insert into ABC_1 (column1, column2) select column1, column2 from ABC
set identity_insert ABC_1 off
Solution 3
If you want to duplicate the table with all its constraints & keys follows this below steps:
- Open the database in SQL Management Studio.
- Right-click on the table that you want to duplicate.
- Select Script Table as -> Create to -> New Query Editor Window. This will generate a script to recreate the table in a new query window.
- Change the table name and relative keys & constraints in the script.
- Execute the script.
Then for copying the data run this below script:
SET IDENTITY_INSERT DuplicateTable ON
INSERT Into DuplicateTable ([Column1], [Column2], [Column3], [Column4],... )
SELECT [Column1], [Column2], [Column3], [Column4],... FROM MainTable
SET IDENTITY_INSERT DuplicateTable OFF
Solution 4
1st option
select *
into ABC_1
from ABC;
2nd option: use SSIS, that is right click on database in object explorer > all tasks > export data
- source and target: your DB
- source table: ABC
- target table: ABC_1 (table will be created)
Solution 5
This is another option:
select top 0 * into <new_table> from <original_table>
![sequel.learner](https://i.stack.imgur.com/Jy4rQ.jpg?s=256&g=1)
sequel.learner
Learning SQL using SQL Server 2008. Know a little bit. Here to learn more. Hope that I can help others like me someday. On the side - I feel that a good way to test an answer/comment in SO is by asking - Would you say that in a Job interview ? If the answer is no, then you should probably change your answer/comment.
Updated on October 01, 2021Comments
-
sequel.learner almost 3 years
I have a table ABC in a database DB. I want to create copies of ABC with names ABC_1, ABC_2, ABC_3 in the same DB. How can I do that using either Management Studio (preferably) or SQL queries ?
This is for SQL Server 2008 R2.
-
sequel.learner over 11 yearsPerfect answer for my problem.
-
Simon Green over 8 yearsDoesn't work completely.... calculated columns in the original aren't copied as the same in the target. Also, PK and Default constraints aren't copied either
-
Mahmoud Gamal over 8 years@SimonGreen - You can post a new question for this problem with your code.
-
user5855178 over 7 yearsOf course it doesn't work completely. He warned constraints don't get copied, nor do primary keys, or default values. The command he offers creates a new table with the same column structure (just like he said) and inserts all data into the new table for you.
-
Alekzander almost 7 yearsThis request copies table without data. User asked to copy tables at all.
-
Thymine over 5 yearsI find using
SELECT TOP(0) *
cleaner than the always-falseWHERE
statement method -
BornToCode over 5 yearsAre you sure this method copies the indices as well? I tried it and it didn't.
-
jean about 5 yearsYou must set
IDENTITY_INSERT
to ON to permit set the identity column "manually", you mixed the order in your example. Also, you must EXPLICIT list your columns -
JohnLBevan about 5 yearsThanks @jean; well spotted after 6 years unnoticed! Fixed.
-
Wade Hatler over 4 yearsThis almost solved the problem for me. The last trick is you can drag the "Columns" folder in the tree in SSMS into the editor to get a list of columns. I have a timestamp column on every table, so I had to get the list of columns, and then remove the timestamp.
-
vusaldev over 2 yearsAlso don't forget about indexes and triggers