如何导入大型MS SQL .sql文件?

Jac*_*ack 229 sql sql-server import

我使用RedGate SQL数据比较并生成.sql文件,因此我可以在本地计算机上运行它.但问题是该文件超过300mb,这意味着我无法复制和粘贴,因为剪贴板将无法处理它,当我尝试在SQL Server Management Studio中打开该文件时,我收到错误关于文件太大了.

有没有办法运行一个大的.sql文件?该文件基本上包含两个新表的数据.

Ray*_*sen 420

从命令提示符,启动sqlcmd:

sqlcmd -S <server> -i C:\<your file here>.sql 
Run Code Online (Sandbox Code Playgroud)

只需替换<server>SQL框的位置和<your file here>脚本的名称即可.不要忘记,如果您使用的是SQL实例,则语法为:

sqlcmd -S <server>\instance.
Run Code Online (Sandbox Code Playgroud)

以下是您可以传递sqlcmd的所有参数的列表:

Sqlcmd            [-U login id]          [-P password]
  [-S server]            [-H hostname]          [-E trusted connection]
  [-d use database name] [-l login timeout]     [-t query timeout] 
  [-h headers]           [-s colseparator]      [-w screen width]
  [-a packetsize]        [-e echo input]        [-I Enable Quoted Identifiers]
  [-c cmdend]            [-L[c] list servers[clean output]]
  [-q "cmdline query"]   [-Q "cmdline query" and exit] 
  [-m errorlevel]        [-V severitylevel]     [-W remove trailing spaces]
  [-u unicode output]    [-r[0|1] msgs to stderr]
  [-i inputfile]         [-o outputfile]        [-z new password]
  [-f  | i:[,o:]] [-Z new password and exit] 
  [-k[1|2] remove[replace] control characters]
  [-y variable length type display width]
  [-Y fixed length type display width]
  [-p[1] print statistics[colon format]]
  [-R use client regional setting]
  [-b On error batch abort]
  [-v var = "value"...]  [-A dedicated admin connection]
  [-X[1] disable commands, startup script, environment variables [and exit]]
  [-x disable variable substitution]
  [-? show syntax summary] 
Run Code Online (Sandbox Code Playgroud)

  • 这对我有用,但我不得不在最后摆脱-o (11认同)
  • 只是确认我刚刚使用此命令运行了1GB的sql文件 (9认同)
  • 我有一些插入,例如: `INSERT INTO table_name (text) VALUES ('some text bla bla 'bla' text')` 。我的引号有错误。那么我必须使用什么(哪个参数)来处理这个错误?谢谢 (3认同)
  • 它无法运行 75G sql 文件。 (3认同)

小智 61

我有完全相同的问题,并且已经挣扎了一段时间,然后终于找到了解决方案,即将-a参数设置sqlcmd为更改其默认数据包大小:

sqlcmd -S [servername] -d [databasename] -i [scriptfilename] -a 32767
Run Code Online (Sandbox Code Playgroud)

  • +1用于在示例中添加-d选项.我在我的sql文件中使用了"USE [DBNAME]".这也很有效.不确定哪种方法更好或更首选 (3认同)
  • 谢谢.我使用以下命令作为连接数据库所需的用户名和密码.`sqlcmd -S <Servername> -U <用户名> -P <密码> -d <数据库名> -i <文件名> (3认同)

Yig*_*sel 19

您也可以使用此工具.这真的很有用.

BigSqlRunner


Sya*_*mar 13

  1. 使用管理员权限获取命令提示符
  2. 将目录更改为.sql文件存储的位置
  3. 执行以下命令

    sqlcmd -S 'your server name' -U 'user name of server' -P 'password of server' -d 'db name'-i script.sql


小智 7

我正在使用MSSQL Express 2014,但没有一个解决方案适合我.他们都只是崩溃了SQL.因为我只需要运行一个带有许多简单插入语句的一个脚本脚本,我通过编写一个小控制台应用程序作为最后的手段:

class Program
{
    static void Main(string[] args)
    {
        RunScript();
    }

    private static void RunScript()
    {
        My_DataEntities db = new My_DataEntities();

        string line;

        System.IO.StreamReader file =
           new System.IO.StreamReader("c:\\ukpostcodesmssql.sql");
        while ((line = file.ReadLine()) != null)
        {
            db.Database.ExecuteSqlCommand(line);
        }

        file.Close();
    }
}
Run Code Online (Sandbox Code Playgroud)