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

前端 未结 7 1581
情书的邮戳
情书的邮戳 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:05

    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
    

提交回复
热议问题