SQL Server copy full table definition

隐身守侯 提交于 2019-12-10 16:40:01

问题


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



来源:https://stackoverflow.com/questions/4616707/sql-server-copy-full-table-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!