这可能很简单。但由于我是 powershell 处理 sql 脚本的新手,所以我在这里有一个问题。
连接到数据库后,我们如何检索该特定数据库中列出的所有架构?有人可以提供一个实现此目的的示例脚本吗?
答案1
# I like to use SMO. There are other ways, arguably just as good.
# You need to make sure that the SMO library is loaded.
# If it already is loaded, you don't need this line.
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
# I'm using integrated security.
# If you aren't, you will need to add a username and password here.
# substitute your server name in place of localhost
$server = New-Object ([Microsoft.SQLServer.Management.SMO.Server]) "localhost"
# Substitute your database name in place of
AdventureWorks $Database = $server.Databases | where {$_.Name -match "AdventureWorks"}
$Database.Schemas | Format-Table Name
# if you want to see schemas in all databases in one list, this should work
$server.Databases.Schemas | format-table Parent,Name