简体   繁体   English

如何将 sql 数据文件导入 SQL Server?

[英]How do I import a sql data file into SQL Server?

我有一个 .sql 文件,我正在尝试将其导入 SQL Server 2008。执行此操作的正确方法是什么?

If your file is a large file, 50MB+, then I recommend you use sqlcmd, the command line utility that comes bundled with SQL Server.如果您的文件是一个 50MB 以上的大文件,那么我建议您使用 sqlcmd,这是 SQL Server 附带的命令行实用程序。 It is easy to use and it handles large files well.它易于使用并且可以很好地处理大文件。 I tried it yesterday with a 22GB file using the following command:我昨天使用以下命令尝试使用 22GB 文件:

sqlcmd -S SERVERNAME\INSTANCE_NAME -i C:\path\mysqlfile.sql -o C:\path\output_file.txt

The command above assumes that your server name is SERVERNAME, that you SQL Server installation uses the instance name INSTANCE_NAME, and that windows auth is the default auth method.上面的命令假设您的服务器名称是 SERVERNAME,您的 SQL Server 安装使用实例名称 INSTANCE_NAME,并且 windows auth 是默认的身份验证方法。 After execution output.txt will contain something like the following:执行后 output.txt 将包含如下内容:

...
(1 rows affected)
Processed 100 total records

(1 rows affected)
Processed 200 total records

(1 rows affected)
Processed 300 total records
...

use readfileonline.com if you need to see the contents of huge files.如果您需要查看大文件的内容,请使用 readfileonline.com。

UPDATE更新

This link provides more command line options and details such as username and password:此链接提供更多命令行选项和详细信息,例如用户名和密码:

https://dba.stackexchange.com/questions/44101/importing-sql-server-database-from-a-sql-file https://dba.stackexchange.com/questions/44101/importing-sql-server-database-from-a-sql-file

If you are talking about an actual database (an mdf file) you would Attach it如果你在谈论一个实际的数据库(一个 mdf 文件),你会Attach

.sql files are typically run using SQL Server Management Studio. .sql文件通常使用 SQL Server Management Studio 运行。 They are basically saved SQL statements, so could be anything.它们基本上是保存的 SQL 语句,所以可以是任何东西。 You don't "import" them.你不“导入”它们。 More precisely, you "execute" them.更准确地说,你“执行”它们。 Even though the script may indeed insert data.即使脚本可能确实插入了数据。

Also, to expand on Jamie F's answer, don't run a SQL file against your database unless you know what it is doing.另外,为了扩展 Jamie F 的答案,除非您知道它在做什么,否则不要针对您的数据库运行 SQL 文件。 SQL scripts can be as dangerous as unchecked exe's SQL 脚本可能与未经检查的 exe 一样危险

  1. Start SQL Server Management Studio启动 SQL Server Management Studio
  2. Connect to your database连接到您的数据库
  3. File > Open > File and pick your file文件 > 打开 > 文件并选择您的文件
  4. Execute it执行它

Try this process -试试这个过程——

Open the Query Analyzer打开查询分析器

Start --> Programs --> MS SQL Server --> Query Analyzer开始 --> 程序 --> MS SQL Server --> 查询分析器

Once opened, connect to the database that you are wish running the script on.打开后,连接到您希望在其上运行脚本的数据库。

Next, open the SQL file using File --> Open option.接下来,使用 File --> Open 选项打开 SQL 文件。 Select .sql file.选择 .sql 文件。

Once it is open, you can execute the file by pressing F5.打开后,您可以按 F5 执行该文件。

In order to import your .sql try the following steps为了导入您的 .sql 请尝试以下步骤

  1. Start SQL Server Management Studio启动 SQL Server Management Studio
  2. Connect to your Database连接到您的数据库
  3. Open the Query Editor打开查询编辑器
  4. Drag and Drop your .sql File into the editor将您的 .sql 文件拖放到编辑器中
  5. Execute the import执行导入

A .sql file is a set of commands that can be executed against the SQL server. .sql 文件是一组可以针对 SQL 服务器执行的命令。

Sometimes the .sql file will specify the database, other times you may need to specify this.有时 .sql 文件会指定数据库,有时您可能需要指定它。

You should talk to your DBA or whoever is responsible for maintaining your databases.您应该与您的 DBA 或负责维护您的数据库的人员交谈。 They will probably want to give the file a quick look.他们可能想快速浏览一下文件。 .sql files can do a lot of harm, even inadvertantly. .sql 文件会造成很多危害,即使是无意中也是如此。

See the other answers if you want to plunge ahead.如果您想继续前进,请参阅其他答案。

There is no such thing as importing in MS SQL.在 MS SQL 中没有导入这样的东西。 I understand what you mean.我明白你的意思。 It is so simple.就这么简单。 Whenever you get/have a something.SQL file, you should just double click and it will directly open in your MS SQL Studio.每当你得到/拥有一个 something.SQL 文件时,你应该双击它,它会直接在你的 MS SQL Studio 中打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM