尝试使用 PowerShell 更改现有存储过程时出错

Error when trying to alter an existing stored procedure with PowerShell

我按照这篇 MSDN 文章使用 PowerShell 更改现有存储过程: https://msdn.microsoft.com/en-us/library/ms162190.aspx
无需阅读

在此示例中,以下行会引发错误:

$sp = New-Object -TypeName Microsoft.SqlServer.Management.SMO.StoredProcedure -argumentlist $db, "GetUserPhoneForService"

错误是

New-Object : The type [Microsoft.SqlServer.Management.SMO.StoredProcedure] cannot be found. Be sure you loaded the assembly containing this type.

(我把报错信息翻译成德文,所以措辞可能有点不同)

简单更新等其他数据库操作没有任何问题。

您需要加载 Sql Powershell 提供程序。使用 Get-Module -ListAvailable 查看所有提供的模块。像这样用 Import-Module 加载 sqlps,

import-module sqlps