在 SQL Server 中使用类似于 INFORMATION_SCHEMA.VIEW_DEFINITION 的内容创建表
是否有标准方法来检索 SQL Server 中表的“CREATE TABLE..”定义?
我可以向 INFORMATION_SCHEMA 询问视图和函数/过程的定义,所以我认为为表获得相同的定义是很自然的,但我没有找到任何东西。
Is there a standard method to retrieve the 'CREATE TABLE..' definition of a table in SQL Server?
I can ask INFORMATION_SCHEMA for definitions of views and functions/procedures, so I thought it would be natural to get the same for tables, but I didn't find anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的 -
INFORMATION_SCHEMA.TABLES
应该是您正在寻找的内容。它会给你类似的信息:
如果你想了解表的列,请查看
INFORMATION_SCHEMA.COLUMNS
。将为您提供指定表的列的所有详细信息。
Yes -
INFORMATION_SCHEMA.TABLES
should be what you're looking for.It will give you something like:
If you want to know about the columns of a table, look at
INFORMATION_SCHEMA.COLUMNS
.will give you all the details for the columns for the table specified.
对视图和代码使用 INFORMATION_SCHEMA 将失败。数据限制为 nvarchar(4000),因此不会读取更长的内容。使用 sys.sql_modules 或 OBJECT_DEFINITION。
对于桌子来说,这就更困难了。 “表”由列、约束、索引、可能的规则、UDT(我忘记了什么吗?)组成。这就是为什么 SSMS 与视图或存储过程相比具有如此多的表脚本选项。
我建议分析 SSMS 并希望它不使用 SMO...或者通过 CLR 代码甚至 xp_cmdshell 使用 SMO。
Using INFORMATION_SCHEMA for views and code will fail. The data is limited to nvarchar(4000), so longer stuff will not be read. Use sys.sql_modules or OBJECT_DEFINITION.
For tables, it's more difficult. A "table" consists of columns, constraints, indexes, possibly rules, UDTs (did I forget anything?). This is why SSMS has so many table scripting options compared to a view or stored proc.
I'd suggest profiling SSMS and hope it doesn't use SMO... or use SMO via CLR code or even xp_cmdshell.
您可以使用 .NET 中的 SMO (Microsoft.SqlServer.Smo) 来编写表脚本。
You can use SMO (Microsoft.SqlServer.Smo) from .NET to script the table.