2

sqlcmd 实用程序是否有任何替代方法可以从 .sql 文件创建数据库?

由于表数据列中存在字符,我收到这些语法错误。我的 .sql 文件大小为 8GB,即使我可以修复 sqlcmd 认为不好的记录,如果有更多记录包含包含 % 符号或“(”等数据的列,则需要一段时间来保存并重新打开文件, sqlcmd 不喜欢。我使用了 On Error Ignore 指令,但这并没有太大帮助。

这是我关于同一问题的另一篇文章:

sqlcmd 在解析我的 .sql 文件时遇到错误

4

2 回答 2

0

这是我的自定义解决方案。它只是一个可以执行巨大 .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% 
于 2014-09-26T19:39:57.190 回答
0

有一个免费工具“SQLS Plus”(在http://www.memfix.com上),它类似于用于 SQL Server 的 Oracle SQL Plus。适用于所有 SQL Server 版本。它应该完成这项工作。

于 2018-07-11T09:12:34.207 回答