在 SQL Server 中,如何为给定表生成 CREATE TABLE 语句?
我花了很多时间来解决这个问题,所以本着这篇文章的精神,我将其发布在这里,因为我认为它可能对其他人有用。
如果有人有更好的脚本或需要添加的内容,请发布。
编辑:是的,伙计们,我知道如何在 Management Studio 中执行此操作 - 但我需要能够从另一个应用程序中执行此操作。
I've spent a good amount of time coming up with solution to this problem, so in the spirit of this post, I'm posting it here, since I think it might be useful to others.
If anyone has a better script, or anything to add, please post it.
Edit: Yes guys, I know how to do it in Management Studio - but I needed to be able to do it from within another application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
我将通过支持分区表来改进答案:
使用下面的脚本查找分区方案和分区键:
然后通过在正确的位置添加以下行来更改生成查询:
I'm going to improve the answer by supporting partitioned tables:
find partition scheme and partition key using below scritps:
then change the generation query by adding below line at the right place:
感谢@Blorgbeard 分享了他的剧本。 我一定会为它添加书签,以备不时之需。
是的,您可以在表上“右键单击”并编写
CREATE TABLE
脚本,但是:通过将此脚本转换为存储过程,并与包装器脚本结合使用,您将有一个很好的自动化方法将表设计转储到源代码控制等中。
数据库代码的其余部分(SP、FK 索引、触发器等)将位于无论如何,源代码控制;)
Credit due to @Blorgbeard for sharing his script. I'll certainly bookmark it in case I need it.
Yes, you can "right click" on the table and script the
CREATE TABLE
script, but:With this script converted into a stored procedure, and combined with a wrapper script you would have a nice automated way to dump your table design into source control etc.
The rest of your DB code (SP's, FK indexes, Triggers etc) would be under source control anyway ;)
我注意到 - 在 INFORMATION_SCHEMA.COLUMNS 视图中,CHARACTER_MAXIMUM_LENGTH 为图像和文本等字段类型提供的大小为 2147483647 (2^31-1)。 ntext 是 2^30-1 (是双字节 unicode 等)。
此大小包含在此查询的输出中,但对于 CREATE 语句中的这些数据类型无效(它们根本不应该具有最大大小值)。 因此,除非手动更正由此产生的结果,否则 CREATE 脚本将无法在给定这些数据类型的情况下工作。
我想可以修复脚本来解决这个问题,但这超出了我的 SQL 能力。
Something I've noticed - in the INFORMATION_SCHEMA.COLUMNS view, CHARACTER_MAXIMUM_LENGTH gives a size of 2147483647 (2^31-1) for field types such as image and text. ntext is 2^30-1 (being double-byte unicode and all).
This size is included in the output from this query, but it is invalid for these data types in a CREATE statement (they should not have a maximum size value at all). So unless the results from this are manually corrected, the CREATE script won't work given these data types.
I imagine it's possible to fix the script to account for this, but that's beyond my SQL capabilities.
-- 或者您可以创建一个存储过程...首先创建 Id
-- 第二个不创建 ID INSERTION
-- or you could create a stored procedure ... first with Id creation
-- and second without iD INSERTION
基于 Hubbitus 回答的查询。
要将删除表(如果存在)与创建结合使用,如下所示:
A query based on Hubbitus answer.
To combine drop table (if exists) with create use like this:
我意识到已经过去很长时间了,但我想我还是要补充一下。 如果您只需要表,而不需要创建表语句,则可以使用
将表复制到新数据库
I realise that it's been a very long time but thought I'd add anyway. If you just want the table, and not the create table statement you could use
to copy the table to a new DB
如果您使用的是 Management Studio 并打开了查询分析器窗口,您可以将表名称拖到查询分析器窗口中,然后...宾果! 你得到了表脚本。
我没有在 SQL2008 中尝试过这个
If you are using management studio and have the query analyzer window open you can drag the table name to the query analyzer window and ... bingo! you get the table script.
I've not tried this in SQL2008
我修改了接受的答案,现在它可以获取包含特定模式中的主键和外键的命令。
I modified the accepted answer and now it can get the command including primary key and foreign key in a certain schema.
我包括计算列的定义
I include definitions for computed columns
在经典 asp 中显示创建表(处理约束、主键、复制表结构和/或数据...)
Show create table in classic asp (handles constraints, primary keys, copying the table structure and/or data ...)
我已经修改了上面的版本以运行所有表并支持新的 SQL 2005 数据类型。 它还保留主键名称。 仅适用于 SQL 2005(使用交叉应用)。
更新: 添加了对 XML 数据类型的处理
更新 2: 修复了 1) 存在多个同名但架构不同的表,2) 存在多个表的情况具有相同名称的 PK 约束
I've modified the version above to run for all tables and support new SQL 2005 data types. It also retains the primary key names. Works only on SQL 2005 (using cross apply).
Update: Added handling of the XML data type
Update 2: Fixed cases when 1) there is multiple tables with the same name but with different schemas, 2) there is multiple tables having PK constraint with the same name
这是我想出的脚本。 它处理标识列、默认值和主键。 它不处理外键、索引、触发器或任何其他聪明的东西。 它适用于 SQLServer 2000、2005 和 2008。
Here's the script that I came up with. It handles Identity columns, default values, and primary keys. It does not handle foreign keys, indexes, triggers, or any other clever stuff. It works on SQLServer 2000, 2005 and 2008.
msdb 论坛中隐藏着一个 Powershell 脚本这将为所有表和相关对象编写脚本:
There is a Powershell script buried in the msdb forums that will script all the tables and related objects:
对架构的支持:
这是一个更新版本,修改了 David 等人的精彩答案。 添加了对命名模式的支持。 应该注意的是,如果各种模式中实际上存在同名的表,则这可能会中断。 另一个改进是使用官方的 QuoteName() 函数。
..
在 Management Studio 中使用:
上面的 sql 代码的一个缺点是,如果您使用 SSMS 对其进行测试,那么长语句不容易阅读。 因此,根据这很有帮助帖子,这是另一个版本,经过一些修改,在单击网格中单元格的链接后更容易查看。 结果更容易识别为数据库中每个表的格式良好的 CREATE TABLE 语句。
不想赘述这一点,但这里是用于比较的功能等效示例输出:
..
不利因素:
应该指出的是,由于缺乏对除 a 之外的 indeces 的支持,我对此仍然相对不满意。首要的关键。 它仍然适合用作简单数据导出或复制的机制。
Support for schemas:
This is an updated version that amends the great answer from David, et al. Added is support for named schemas. It should be noted this may break if there's actually tables of the same name present within various schemas. Another improvement is the use of the official QuoteName() function.
..
For use in Management Studio:
One detractor to the sql code above is if you test it using SSMS, long statements aren't easy to read. So, as per this helpful post, here's another version that's somewhat modified to be easier on the eyes after clicking the link of a cell in the grid. The results are more readily identifiable as nicely formatted CREATE TABLE statements for each table in the db.
Not to belabor the point, but here's the functionally equivalent example outputs for comparison:
..
Detracting factors:
It should be noted that I remain relatively unhappy with this due to the lack of support for indeces other than a primary key. It remains suitable for use as a mechanism for simple data export or replication.
如果您生成脚本的应用程序是 .NET 应用程序,您可能需要考虑使用 SMO(Sql 管理对象)。 请参考此 SQL Team 链接,了解如何使用 SMO脚本对象。
If the application you are generating the scripts from is a .NET application, you may want to look into using SMO (Sql Management Objects). Reference this SQL Team link on how to use SMO to script objects.
另一种具有外键支持的变体在一个声明中:
您可以尝试使用 sqlfiddle: http: //sqlfiddle.com/#!6/e3b66/3/0
One more variant with foreign keys support and in one statement:
You could try in is sqlfiddle: http://sqlfiddle.com/#!6/e3b66/3/0