SQL Server copy full table definition

2019-06-25 04:10发布

问题:

In the Enterprise version of SQL Server it is possible to generate the create statement for a pre-existing table by using 'Script Table as', "Create to". This generates the full create statement for the table including indices. Is there any way to do this in T-SQL and therefore be able to copy a table definition to a new table?

回答1:

Nothing simply available out of the box in straight T-SQL which exactly replicates what SSMS does.

However, you can get to this through the metadata (INFORMATION_SCHEMA.COLUMNS etc), and I have done this fairly successfully. Note that the indexes are not in INFORMATION_SCHEMA - they are in sys.indexes.

Have a look at this article which probably has most of what you want.



回答2:

Create the script to a new query window, then modify the database name and run it