これが私のカスタム ソリューションです。これは、巨大な .sql ファイルを実行できる単なる PowerShell スクリプトです。
$filePath = "GiganticFile.sql";
$stream = [System.IO.File]::OpenText($filePath);
$connection = New-Object System.Data.SqlClient.SqlConnection;
$connection.ConnectionString = "Persist Security Info=True;User ID=<user>;Password=<password>;Initial Catalog=<database>;Data Source=<server>";
$connection.Open();
$command = New-Object System.Data.SqlClient.SqlCommand;
$command.Connection = $connection;
$statement = "";
$statementsProcessed = 0;
$totalBytes = (New-Object System.IO.FileInfo($filePath)).Length;
$bytesProcessed = 0;
while (-not $stream.EndOfStream) {
$line = $stream.ReadLine();
$bytesProcessed += $line.Length;
if ($line.Trim() -eq "GO") {
try {
$command.CommandText = $statement;
[void]$command.ExecuteNonQuery();
$statement = "";
"{0} statements processed ({1:f} MB) - {2:f}%" -f (++$statementsProcessed), ($bytesProcessed / 1024 / 1024), ($bytesProcessed / $totalBytes * 100);
}
catch {
"!!! ERROR !!!";
$Error;
}
}
else {
$statement += $line + [Environment]::NewLine;
}
}
$stream.Close();
出力サンプル:
137238 statements processed (544.54 MB) - 7.57%
137239 statements processed (544.55 MB) - 7.57%
137240 statements processed (544.56 MB) - 7.57%
137241 statements processed (544.57 MB) - 7.57%
137242 statements processed (544.58 MB) - 7.57%
137243 statements processed (544.59 MB) - 7.57%
137244 statements processed (544.60 MB) - 7.57%
137245 statements processed (544.61 MB) - 7.57%
137246 statements processed (544.63 MB) - 7.58%
137247 statements processed (544.64 MB) - 7.58%
137248 statements processed (544.65 MB) - 7.58%
137249 statements processed (544.66 MB) - 7.58%
137250 statements processed (544.67 MB) - 7.58%
137251 statements processed (544.68 MB) - 7.58%
137252 statements processed (544.69 MB) - 7.58%
137253 statements processed (544.70 MB) - 7.58%