为表和所有扩展属性生成脚本
寻找一种方法为每个表生成单独的脚本并包含任何关系或扩展属性。 (完成这项工作后,我将尝试为存储过程和函数生成脚本)
我知道 Sql-Server UI 生成器(数据库 => 任务 => 生成脚本),但它会生成一个大文件,而不是单个文件。如果有一种方法可以使其生成单独的文件(而不是一次生成一个文件),那就最好了。
我使用 Powershell 包 DBATools 取得了一些有限的成功。以下将创建一个文件,其中包含表和表的扩展属性的创建脚本,但不包含列扩展属性。
$server = "sql01"
$database = "MyDatabase"
$table = "MyTable"
Get-DbaDbTable -SqlInstance $server -Database $database -Table $table | Export-DbaScript -FilePath ($database + "\" + $table +".sql")
Get-DbaDbTable -SqlInstance $server -Database $database -Table $table | Get-DbaExtendedProperty | ForEach-Object { Export-DbaScript -InputObject $_ -FilePath ($database + "\" + $table +".sql") -Append }
Looking for a way to generate individual scripts for each table and include any relationships or extended properties. (After getting this working I will try to generate scripts for stored procedure, and function)
I am aware of the Sql-Server UI Generator (Database=>Tasks=>Generate Scripts) but that does one big file, not individuals. If there is a way to make this produce individual files (with out doing them 1 at a time) that would be best.
I have used Powershell package DBATools with some limited success. The following will make a file that contains create scripts for the table and the table's extended property but not the column extended properties.
$server = "sql01"
$database = "MyDatabase"
$table = "MyTable"
Get-DbaDbTable -SqlInstance $server -Database $database -Table $table | Export-DbaScript -FilePath ($database + "\" + $table +".sql")
Get-DbaDbTable -SqlInstance $server -Database $database -Table $table | Get-DbaExtendedProperty | ForEach-Object { Export-DbaScript -InputObject $_ -FilePath ($database + "\" + $table +".sql") -Append }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
拉尔努在社区网络中给出了答案。
该评论指出了单独保存脚本的选项,多年来我一直在关注它。
The answer was given by Larnu in the commnets.
The comment pointed out the option to save scripts individually and I have been over looking it for years.
虽然您在 SSMS 中找到了该选项,但我想说,使用您尝试过的 dbatools 方法也可以实现这一点。 “秘密武器”是指定控制脚本行为的 ScriptingOptions 对象。
While you found the option in SSMS, I wanted to say that this is also possible using the dbatools approach you tried. The "secret sauce" is specifying a ScriptingOptions object that controls the scripting behavior.