Clone a table from a script

  • I have a table schema in an exported .sql file (text). Is there any way to use that to create an identical table (with the name changed of course)?

    The file was created using script table as -> create to -> file.

    TIA

     

  • If you want the data as well as the table, that method won't work.  If you ONLY want the table schema and don't care about the data, that will work fine; you just need to open the .sql file and find and replace the old name with the new one.

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • Yeah, I want just the schema. But how do I import the sql file into the database to create a new table?

  • That part is easy then -   Run the script.

    The script is just plain TSQL used for re-creating the object.  It can be opened in SSMS or SQLCMD and run against the database.  You can even open it in notepad to review the code behind it.

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • maylar wrote:

    Yeah, I want just the schema. But how do I import the sql file into the database to create a new table?

    Bulk Insert the text file as a single blob into a variable and then execute the variable.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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