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

前端 未结 7 1580
情书的邮戳
情书的邮戳 2020-12-23 02:33

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 (preferab

7条回答
  •  太阳男子
    2020-12-23 03:23

    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
    

提交回复
热议问题