为表和所有扩展属性生成脚本

发布于 2025-01-17 03:28:02 字数 683 浏览 10 评论 0原文

寻找一种方法为每个表生成单独的脚本并包含任何关系或扩展属性。 (完成这项工作后,我将尝试为存储过程和函数生成脚本)

我知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰◇生如夏花灿烂 2025-01-24 03:28:02

拉尔努在社区网络中给出了答案。

该评论指出了单独保存脚本的选项,多年来我一直在关注它。

输入图片此处描述

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.

enter image description here

oО清风挽发oО 2025-01-24 03:28:02

虽然您在 SSMS 中找到了该选项,但我想说,使用您尝试过的 dbatools 方法也可以实现这一点。 “秘密武器”是指定控制脚本行为的 ScriptingOptions 对象。

$so = New-DbaScriptingOption;
$so.ExtendedProperties = $true;

foreach ($table in Get-DbaDbTable -SqlInstance . -Database AdventureWorks2019){
   $path = '{0}.{1}.sql' -f $table.Schema, $table.Name;
   Export-DbaScript -InputObject $table -FilePath $path -ScriptingOptionsObject $so;
}

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.

$so = New-DbaScriptingOption;
$so.ExtendedProperties = $true;

foreach ($table in Get-DbaDbTable -SqlInstance . -Database AdventureWorks2019){
   $path = '{0}.{1}.sql' -f $table.Schema, $table.Name;
   Export-DbaScript -InputObject $table -FilePath $path -ScriptingOptionsObject $so;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文