【问题标题】:Outputting large XML file from SQL with Powershell使用 Powershell 从 SQL 输出大型 XML 文件
【发布时间】:2019-10-10 01:42:23
【问题描述】:

我有一个 Powershell 脚本,它调用 SQL Server 中的存储过程并将生成的 XML 直接输出到文件中。它适用于较小的测试文件,但会随着完整大小(~1.5gb 文件)而下降。

存储过程工作正常。我可以在 SQL Server 中调用它 - 问题是我必须在 SQL Server 中打开它,然后手动将其保存到文件中,然后编辑该文件以删除换行符。当我运行脚本时,它会在最后尝试删除中间文件时崩溃。但是,当我逐行运行它时,它会落在 invoke-sql 行。

#net use S: "\\processStore\projects"

# Create variables
$SQLquery = "DECLARE @return_value int; EXEC @return_value = [XML].[XMLdata];"
$outpath = "D:\MyDocuments\XML\XML files" 
$outfile = "TestOutput"

# Run the SQL command and store the object to a variable. Need to extend the timeout from the default.
$sql = invoke-sqlcmd -Database 'APP2021' -Query $SQLquery -serverinstance 'statdata' -QueryTimeout 100000

cd C:

# Store the SQL output as an interim text file
$sql.{XML_F52E2B61-18A1-11d1-B105-00805F49916B} | out-file -Filepath "$outpath\$outfile.txt" 

# Remove Linebreaks
(Get-Content "$outpath\$outfile.txt" -Raw).Replace("`r`n","") | Set-Content "$outpath\${outfile}_del.xml" -Force
format-xml "$outpath\${outfile}_del.xml" | Set-Content "$outpath\$outfile.xml" -Force

# Delete interim text files
del "$outpath\$outfile.txt"
del "$outpath\${outfile}_del.xml"

当逐行运行时,代码在 invoke-sqlcmd 行出现错误:

invoke-sqlcmd : A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote 
host.)
At line:1 char:8
+ $sql = invoke-sqlcmd -Database 'APP2021' -Query $SQLquery -serverinst ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
    + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

【问题讨论】:

  • 可能是超时问题?
  • 我希望这与此有关,但我不确定如何解决它?我将 QueryTimeout 值调到 400000,但没有帮助。
  • 服务器好像超时了。
  • 知道我能做些什么来阻止它超时吗?
  • 服务器日志有错误吗?

标签: sql-server xml powershell using invoke-sqlcmd


【解决方案1】:


改变 调用-sqlcmd -Database 'APP2021' -Query $SQLquery -serverinstance 'statdata' -QueryTimeout 100000

调用-sqlcmd -Database 'APP2021' -Query $SQLquery -serverinstance 'statdata' -QueryTimeout 0

设置 QueryTimeout = 0 将防止查询超时

【讨论】:

  • 好建议,但我刚刚尝试过,一段时间后Powershell窗口崩溃而没有错误。
猜你喜欢
  • 1970-01-01
  • 2015-11-25
  • 1970-01-01
  • 2019-10-07
  • 1970-01-01
  • 2013-06-22
  • 1970-01-01
  • 2021-11-15
  • 1970-01-01
相关资源
最近更新 更多