sql server 2008 SP2中sqlcmd的替代方案

时间:2012-07-27 19:29:03

标签: sql-server sql-server-2008 sqlcmd

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

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

以下是关于同一问题的另一篇文章:

sqlcmd runs into error while parsing my .sql file

2 个答案:

答案 0 :(得分:0)

这是我的自定义解决方案。它只是一个PowerShell脚本,可以执行巨大的.sql文件。

$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% 

答案 1 :(得分:0)

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