Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 使用SMO脚本程序生成脚本时,如何编写过程脚本?_Sql Server_Powershell_Stored Procedures_Sql Server 2008 R2_Smo - Fatal编程技术网

Sql server 使用SMO脚本程序生成脚本时,如何编写过程脚本?

Sql server 使用SMO脚本程序生成脚本时,如何编写过程脚本?,sql-server,powershell,stored-procedures,sql-server-2008-r2,smo,Sql Server,Powershell,Stored Procedures,Sql Server 2008 R2,Smo,我正在尝试编写Powershell脚本,以生成在数据库中创建存储过程的SQL脚本。以下是我所拥有的: [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO')| out-null Function to-array ($c) { foreach($item in $c) { $result += @($item) } $result } $s = new

我正在尝试编写Powershell脚本,以生成在数据库中创建存储过程的SQL脚本。以下是我所拥有的:

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO')| out-null

Function to-array ($c)
{
    foreach($item in $c)
    {
      $result += @($item)
    }
    $result
}

$s = new-object ('Microsoft.SqlServer.Management.Smo.Server') "LOCALHOST"
$scrp = new-object ('Microsoft.SqlServer.Management.Smo.Scripter') ($s)
$dbs = $s.Databases
$fa = $dbs["FinancialAid"]

$scrp.Options.ScriptData = $True
$scrp.Options.AllowSystemObjects = $False
$scrp.Options.FileName = 'StoredProcedures.sql'
$scrp.Options.ToFileOnly = $True

# Finally the stored procedures

$sparray = to-array($fa.StoredProcedures)
$scrp.EnumScript($sparray)
此操作失败,错误如下:

Exception calling "EnumScript" with "1" argument(s): "Script failed for Server 'LOCALHOST'. "
At H:\Visual Studio 2012\FinancialAidApplication\Financial Aid Application\FinancialAidApplication\main\src\FinancialAidApp
\SQL-Scripts\sp.ps1:28 char:17
+ $scrp.EnumScript <<<< ($sparray)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
使用“1”参数调用“EnumScript”时出现异常:“服务器“LOCALHOST”的脚本失败。”
位于H:\Visual Studio 2012\FinancialAidApplication\FinancialAidApplication\FinancialAidApplication\main\src\FinancialAidApp
\SQL脚本\sp.ps1:28字符:17
+$scrp.EnumScript几件事

  • 尝试进入实际的异常。将“内部异常powershell”放入您喜爱的搜索引擎。现在,这只是一个普通的“出了问题”,很难诊断(对你和我们都是如此!)

  • 尝试
    $sparray=to数组($fa.StoredProcedures |其中{$\u.IsSystemObject-eq$false)


  • 或者您可以完全跳过数组,只使用管道。 (来自我经常使用但经过修改的一部分内容,您可以使用变量)


    有一件事:我不得不使用
    $scrp.EnumScript
    ,因为
    $scrp.Script
    拒绝为“数据”编写脚本(我猜它考虑了过程体数据)@SamuelEdwinWard,你是说你必须修改我的示例才能让它工作吗?如果是这样的话,我会感到困惑。在我将上面的代码更改为服务器和数据库名称后,它工作正常,然后我将其添加为答案。你是对的,你所做的应该工作。我已经将
    ScriptData
    设置为true,我不应该这样做。它一定是一个mis就拿我之前努力解决这个问题时做的来说。
    add-type -AssemblyName "Microsoft.SqlServer.Smo, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
    $s= new-object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList 'LOCALHOST'
    $fa = $s.Databases['FinancialAid']
       $scrp = New-Object ("Microsoft.SqlServer.Management.Smo.Scripter")
       $scrp.Server = $s
       $options = New-Object ("Microsoft.SqlServer.Management.SMO.ScriptingOptions")
       $options.IncludeHeaders = $true
       $options.FileName = 'C:\temp\StoredProcedures.sql'
       $options.AppendToFile = $true
       $options.ToFileOnly = $true
       $options.ScriptBatchTerminator = $true
       $scrp.Options = $options
    $fa.StoredProcedures | foreach {if ($_.IsSystemObject -eq $false) { $scrp.Script($_) }}