Create Clone of Existing Table using script in SQL Server 2008

In this article, you will see how to create a duplicate table with a new name in SQL Server.using script.
  • 3225

Introduction

In this article I will show you the various methods of copying a table with a new name using a script in SQL Server. It takes a lots of time to create new empty table and defining all field names again.

Creating Table in SQL Server

create table StudentDetails
(
roll_num int,
F_Name varchar(15),
L_Name varchar(15),
City varchar(15)
)

Insert values in above table and table will look like below image.

StudentDetails_Table.jpg
 

Copying table using general select into query ignores the indexes and constraints. So generate the script of the table.

Generate Table Script in SQL Server Management Studio

  1. Open Object Explorer . And right click on table whose script is to be created. Suppose you want to generate script for table called Student. Select Script Table as -> CREATE TO ->  New Query Editor Window.

    CreateScript-in-sql.jpg
     
  2. Script is generated for the table called StudentDetails in a new query window.

    Script-in-sql.jpg
     
  3.  Now use this script after changing the name of table in the script which you want the new table to be named.

    ModifyScript-in-sql.jpg
     
  4. Execute above script.

    ExecuteScript.jpg
     
  5. Now refresh the database in which you create the table. Now you can see, the new table is created in deep database.

    NewTable-using-script.jpg

© 2020 DotNetHeaven. All rights reserved.