locked
How to clone a table for testing ? RRS feed

  • Question

  • Hi All,

    I am trying to test a script on a table and I wanted to make an exact copy of the table and name it differently so I can test my script on the new table

    Thanks In advance
    Thursday, April 3, 2014 11:55 PM

Answers

  • You can script your table by right click on it from SSMS and follow Script table as...

    If you want data as well as the schema, you can right click on your database name and click on Tasks>>Generate Scripts>>..  follow the wizard >> last step is to click on Advanced tab to change a property under Options:Types of data to: Schema and data. (The default is Schema Only).

     You can run the script with a modified table name to clone your table and data.

    • Proposed as answer by PrasannaKumarK Friday, April 4, 2014 6:45 AM
    • Marked as answer by Elvis Long Monday, April 14, 2014 2:15 AM
    Friday, April 4, 2014 12:17 AM
  • I think best thing would be to script out the table using generate scripts wizard and apply it back after renaming (find and replace old name with new name) to preserve original constraints etc

    http://blog.sqlauthority.com/2012/07/18/sql-server-generate-script-for-schema-and-data-sql-in-sixty-seconds-021-video/


    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs


    • Edited by Visakh16MVP Friday, April 4, 2014 6:51 AM
    • Proposed as answer by Elvis Long Tuesday, April 8, 2014 10:58 AM
    • Marked as answer by Elvis Long Monday, April 14, 2014 2:15 AM
    Friday, April 4, 2014 6:51 AM

All replies

  • You can script your table by right click on it from SSMS and follow Script table as...

    If you want data as well as the schema, you can right click on your database name and click on Tasks>>Generate Scripts>>..  follow the wizard >> last step is to click on Advanced tab to change a property under Options:Types of data to: Schema and data. (The default is Schema Only).

     You can run the script with a modified table name to clone your table and data.

    • Proposed as answer by PrasannaKumarK Friday, April 4, 2014 6:45 AM
    • Marked as answer by Elvis Long Monday, April 14, 2014 2:15 AM
    Friday, April 4, 2014 12:17 AM
  • Does select * into work for you?
    • Proposed as answer by Jitesh00JJ Friday, April 4, 2014 6:51 AM
    • Unproposed as answer by Jitesh00JJ Friday, April 4, 2014 6:54 AM
    Friday, April 4, 2014 6:32 AM
  • create table t1 
    ( id int,
      name varchar(10)
      )
     insert t1 values (1,'swap')
     insert t1 values (1,'swap1')
     insert t1 values (1,'swap2')
     insert t1 values (1,'swap3')
     insert t1 values (1,'swap4')
    --within same database     
    select * into T2 from t1
         
         
    -- to different database    
    select * into test.dbo.T1 
    from test1.dbo.t1  

    Hope this helps!! 
    • Proposed as answer by ssalun Friday, April 4, 2014 6:45 AM
    Friday, April 4, 2014 6:45 AM
  • I think best thing would be to script out the table using generate scripts wizard and apply it back after renaming (find and replace old name with new name) to preserve original constraints etc

    http://blog.sqlauthority.com/2012/07/18/sql-server-generate-script-for-schema-and-data-sql-in-sixty-seconds-021-video/


    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs


    • Edited by Visakh16MVP Friday, April 4, 2014 6:51 AM
    • Proposed as answer by Elvis Long Tuesday, April 8, 2014 10:58 AM
    • Marked as answer by Elvis Long Monday, April 14, 2014 2:15 AM
    Friday, April 4, 2014 6:51 AM
  • Does select * into work for you?

    Nope. that would  not replicate indexes, constraints etc as in main table so it wont be an exact clone

    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

    Friday, April 4, 2014 6:52 AM