[英]Error executing SQL script via SMO in PowerShell
I have some code that loads a script to a variable, and then I pass the variable to an SMO object. 我有一些代码将脚本加载到变量,然后我将变量传递给SMO对象。 I get the following exception:
我得到以下异常:
Exception calling "ExecuteWithResults" with "1" argument(s): "Execute with results failed for Database 'Russell_Test'. "
使用“1”参数调用“ExecuteWithResults”的异常:“对数据库'Russell_Test'执行结果失败。”
How can I fix this problem? 我该如何解决这个问题?
Below is the relevant portion of the code. 以下是代码的相关部分。
# Load Smo and referenced assemblies.
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.ConnectionInfo');
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Management.Sdk.Sfc');
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO');
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMOExtended');
Try{
$server = New-Object Microsoft.SqlServer.Management.Smo.Server $serverName;
$db = $server.Databases.Item($databaseName);
$result = $db.ExecuteWithResults($createScript);
$result | Out-File -Append -FilePath $outputFile;
}
Catch
{
[system.exception]
$_.Exception | Out-File -Append -FilePath $outputFile
}
Here's how to do it: 这是怎么做的:
# If we cast $Content as a [String], all the newlines are lost
# If we don't cast it, it becomes an array and batching breaks
$Content = (Get-Content $SqlScript) -join [Environment]::NewLine
# When using GO, we must set it up as a StringCollection, not a List or Array
$Batch = New-Object -TypeName:Collections.Specialized.StringCollection
$Batch.AddRange($Content)
$result = $Database.ExecuteWithResults($Batch)
Thanks for the help. 谢谢您的帮助。 Ultimately the SMO option would not work so I did this solution:
最终SMO选项不起作用所以我做了这个解决方案:
# Deploy table update scripts
$createScript = Get-Content $scriptFile.FullName | Out-String
################# Script execution to capture errors/messages/warnings ##################
$createScriptList = [regex]::Split($createScript, '\bGO')
$cn2 = new-object system.data.SqlClient.SQLConnection("Data Source=$serverName;Integrated Security=SSPI;Initial Catalog=$databaseName;Connection Timeout=600;Max Pool Size=10");
$cn2.Open();
foreach ($cSL in $createScriptList)
{
Try{
$cmd = new-object system.data.sqlclient.sqlcommand($cSL, $cn2);
$cmd.ExecuteScalar() | Out-File -Append -FilePath $outputFile;
}
Catch
{
[system.exception]
$_.Exception | Out-File -Append -FilePath $outputFile
}
}
$cn2.Close();
###############################################################################################
Try to use Invoke-SqlCmd
cmdlet. 尝试使用
Invoke-SqlCmd
cmdlet。 This cmdlet allows you to run T-SQL code or commands supported be the SQLCMD utility. 此cmdlet允许您运行SQLCMD实用程序支持的T-SQL代码或命令。
Try{
Invoke-SqlCmd `
-Query $createScript `
-ServerInstance $serverName `
-Database $databaseName `
-ErrorAction Stop `
}
Catch
{
[system.exception]
$_.Exception | Out-File -Append -FilePath $outputFile
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.