在 SQL Server Management Studio 中导入数据库(SQL 文件)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22488972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Import database (SQL file) in SQL Server Management Studio
提问by Matt
I've created the structure of my database first in PhpMyAdmin and exported it to a .sql file. Now I'm looking everywhere in SQL Server Management Studio where I can import/add the data in a new database.
我首先在 PhpMyAdmin 中创建了我的数据库结构并将其导出到 .sql 文件。现在我在 SQL Server Management Studio 中到处寻找我可以在新数据库中导入/添加数据的地方。
Does anybody where to look or what to click? I'm using the 2014 version (CTP2)
有没有人在哪里看或点击什么?我使用的是 2014 版 (CTP2)
回答by Carley
If you have a .sql file which contains SQL statements, you can just copy and paste the contents (or open the file in a query window) and run it. This assumes it has all of the create table etc. statements to create the schema/structure and not just insert statements for the data.
如果您有一个包含 SQL 语句的 .sql 文件,您只需复制并粘贴内容(或在查询窗口中打开文件)并运行它。这假设它具有创建模式/结构的所有创建表等语句,而不仅仅是数据的插入语句。
Check the top of the file to make sure that it is first selecting the correct database, if not add a USE statement to select the correct database.
检查文件的顶部以确保它首先选择了正确的数据库,如果没有添加 USE 语句以选择正确的数据库。
You didn't say how big the file was, but if it is quite large and has the insert statements (data as well as schema), then you'll probably want to run by CLI using sqlcmd command. Much faster and SSMS won't freak out.
您没有说明文件有多大,但如果文件很大并且包含插入语句(数据和架构),那么您可能希望通过 CLI 使用 sqlcmd 命令运行。快得多,SSMS 不会吓坏了。
Another alternative option to running the .sql file/code is to set up a data source for mysql and just use odbc to access the database itself.
运行 .sql 文件/代码的另一个替代选项是为 mysql 设置数据源,然后仅使用 odbc 访问数据库本身。
Bear in mind that there are real and very annoying differences between mysql and t-sql that can make migration a pain. If you're just creating a few tables, it may not be an issue, but if there are a ton of tables with lots of fields of different data types, you may run into issues.
请记住,mysql 和 t-sql 之间确实存在非常令人讨厌的差异,这可能会使迁移变得痛苦。如果您只是创建几个表,这可能不是问题,但如果有大量包含许多不同数据类型字段的表,您可能会遇到问题。
回答by SCJohnson243
Open the server, open "Databases" and right click the database, go to "Tasks" and then Import Data...
打开服务器,打开“数据库”并右键单击数据库,转到“任务”,然后导入数据...
回答by CadSquad
I have had the most 'trouble free' success importing to SQL via a flat file method (comma delimited .txt file), the only stipulation when creating a flat file (i.e from Access) make sure the text identifier is set to {none} and not "". To import the file: in the SQL Server Management Studio right click on Databases and create a new database. Then right click on the new database -> Tasks -> Import Data... The import window opens: in the DATA SOURCE option select Flat File Source and select the .txt file...click NEXT. In the DESTINATION field select SQL Server Native Client 11.0 and go through the import process. This worked very well for me.
我通过平面文件方法(逗号分隔的 .txt 文件)导入到 SQL 中获得了最“无故障”的成功,创建平面文件时的唯一规定(即从 Access)确保文本标识符设置为 {none}并不是 ””。导入文件:在 SQL Server Management Studio 中右键单击 Databases 并创建一个新数据库。然后右键单击新数据库 -> 任务 -> 导入数据... 导入窗口打开:在 DATA SOURCE 选项中选择 Flat File Source 并选择 .txt 文件...单击 NEXT。在 DESTINATION 字段中选择 SQL Server Native Client 11.0 并完成导入过程。这对我来说非常有效。