Duplicating tables with TSQL

  • Does anyone know of a way looking up the names of all the user tables in a database and creating an indentical copy of the structure of each of those tables (eg, tblOrders copied to tblOrders_Copy) in one go without having to manually do each table individually?

    Thanks.

     

  • Hello,

    You can script the objects available currently and add up the word "_Copy" to the scripted objects and execute the same.

    Thanks and have a nice day!!!


    Lucky

  • Write SQL to generate SQL:

    SELECT 'SELECT * INTO ' + name + '_Copy FROM ' + name + ' WHERE 0 = 1'

    FROM sysobjects

    WHERE type = 'U'

    Take the output of that and run it.

     

  • Thanks guys - that's done the trick!

     

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply