sequel.learner
sequel.learner

Reputation: 3941

SQL Server - Create a copy of a database table and place it in the same database?

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.

Upvotes: 200

Views: 420822

Answers (9)

Murad
Murad

Reputation: 1

Stored Procedure to Backup a Table in SQL Server

-- This stored procedure creates a backup of a specified table
-- by copying its data into a new table with a timestamped name.

CREATE OR ALTER PROCEDURE sp_BackupTable(@tableName NVARCHAR(500))
AS
BEGIN
    -- Declare variables for dynamic SQL and timestamp
    DECLARE @sql NVARCHAR(2000); -- Dynamic SQL statement
    DECLARE @nowDate NVARCHAR(50) = FORMAT(GETDATE(), 'yyyyMMdd_HHmmss'); -- Current timestamp

    -- Construct the dynamic SQL statement
    SET @sql = N'SELECT * INTO ' + QUOTENAME(@tableName + '_Backup_' + @nowDate) + 
               N' FROM ' + QUOTENAME(@tableName) + N';';

    -- Execute the dynamic SQL statement
    EXEC sp_executesql @sql;
END
GO
EXEC sp_BackupTable @tableName = 'TABLE_NAME';

Upvotes: 0

kkid
kkid

Reputation: 81

In Case you want to do it N times (may be not practical in real world), you might try this:

declare @counter int
set @counter = 2
declare @tname NVARCHAR(100)
DECLARE @SQString NVARCHAR(MAX)
while(@counter <= 20) -- duplicate 20 times
begin
SET @tname= concat('Table_',@counter)
SET @SQString = 'select * into ' + @tname + ' from Table_1'
EXEC (@SQString)
set @counter = @counter + 1
End

Upvotes: 0

JohnLBevan
JohnLBevan

Reputation: 24410

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

Upvotes: 34

Rousonur Jaman
Rousonur Jaman

Reputation: 1261

If you want to duplicate the table with all its constraints & keys follows this below steps:

  1. Open the database in SQL Management Studio.
  2. Right-click on the table that you want to duplicate.
  3. Select Script Table as -> Create to -> New Query Editor Window. This will generate a script to recreate the table in a new query window.
  4. Change the table name and relative keys & constraints in the script.
  5. 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

Upvotes: 29

Chris D
Chris D

Reputation: 27

You need to write SSIS to copy the table and its data, constraints and triggers. We have in our organization a software called Kal Admin by kalrom Systems that has a free version for downloading (I think that the copy tables feature is optional)

Upvotes: 1

Keni
Keni

Reputation: 65

This is another option:

select top 0 * into <new_table> from <original_table>

Upvotes: 3

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79909

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;

Upvotes: 412

gasroot
gasroot

Reputation: 523

use sql server manegement studio or netcat and that will be easier to manipulate sql

Upvotes: -2

Claude
Claude

Reputation: 1774

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)

Upvotes: 7

Related Questions