2016-12-12 20 views
0

こんにちは皆、私は現在、クエリの結果をタブ区切りCSVファイルにスケジュールで自動的に書き込もうとしています。現在、私はこの自動的にファイルにSQLクエリ結果を出力します

を行うために
$results = Invoke-SQLCmd -ServerInstance $Server -Database $Database -query "select distinct * from WHIProducts" 
$results | ConvertTo-csv -NoTypeInformation -Delimiter "`t" | Out-File "$inventorypath\inventory_$date\$filename" -fo -en ascii 

をPowerShellを使用していますこの問題では、結果は私がSystem.OutOfMemoryExceptionにエラーを取得していますので、大きいということです。私はmaxmemorypershellを増やしてみましたが、私はまだ同じエラーが発生します。これは自動的にSSMSに入り、手動で行うだけでは不十分です。何か案は?

〜170k行を印刷しようとしています。もっと最終的には、おそらく約300kまであります。ここにpowershellエラーがあります。

ConvertTo-csv: 'System.OutOfMemoryException'タイプの例外が でした。 C:¥Users¥pmaho¥Dropbox¥MASSFILEStest¥scripts¥daily_inventory .ps1:59 char:12 + $ results | ConvertTo-csv -NoTypeInformation -Delimiter "t" |アウトフィル... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~ + CategoryInfo:NotSpecified:(:) [をConvertTo-CSV]、OutOfMemoryException例外 + FullyQualifiedErrorId:System.OutOfMemoryExceptionに、Microsoft.PowerShell.Commands.Co nvertToCsvCommand -

私はSQLを使用していますServer Express Edition

+1

カップルの事:あなたが個別にクエリを実行する場合は1)、*はどのように多くの行あなたが返すように期待している* 2)あなたが受け取る 'System.OutofMemoryException'エラー - ?あなたはそれを逐語的に提供できますか? – gravity

+1

[編集]をクリックしてその情報をすべて実際の質問に入れると、誰にとっても有益です。 – gravity

+0

それはすべて問題です。 –

答えて

3

Invoke-SqlCmdの出力をExport-CSVコマンドレットにパイプしてみます。ここにスクリプトがあります。これを容易質問は答えることになるだろう

Push-Location; Import-Module SQLPS -DisableNameChecking; Pop-Location 

$SQLServer = "localhost\inst1" 
$DBName = "ExportCSVTesting" 
$ExportFile = "C:\Users\BIGRED-7\Documents\Git\csvfiles\addresses.csv" 
$Counter = 0 

while ($true) 
{ 
    # Remove the export file 
    if (Test-Path -Path $ExportFile -PathType Leaf) { 
     Remove-Item $ExportFile -Force 
    } 

    # Clear the buffer cache to make sure each test is done the same 
    $ClearCacheSQL = "DBCC DROPCLEANBUFFERS" 
    Invoke-Sqlcmd -ServerInstance $SQLServer -Query $ClearCacheSQL 

    # Export the table through the pipeline and capture the run time. Only the export is included in the run time. 
    $ExportSQL = "SELECT * FROM [addresses] ;" 

    $sw = [Diagnostics.Stopwatch]::StartNew() 
    Invoke-Sqlcmd -ServerInstance $SQLServer -Database $DBName -Query $ExportSQL | Export-CSV -Path $ExportFile -NoTypeInformation 
    $sw.Stop() 
    $sw.Elapsed 
    $Milliseconds = $sw.ElapsedMilliseconds 

    # Get a row count for display 
    $RowCountSQL = "SELECT COUNT(0) AS [Count] FROM [addresses] ;" 
    $RowCount = Invoke-Sqlcmd -ServerInstance $SQLServer -Database $DBName -Query $RowCountSQL 
    $RowCount = $RowCount.Count 

    $Counter++ 
    Write-Output ("Run $Counter of RowCount: $RowCount") 

    # Log the run statistics 
    $StatsSQL = "INSERT INTO [RunStats] (Counter,Milliseconds,Notes) VALUES ($RowCount,$Milliseconds,'Pipeline')" 
    Invoke-Sqlcmd -ServerInstance $SQLServer -Database $DBName -Query $StatsSQL 
} 
関連する問題