Microsoft SQL Server:如何从数据库导出数据并将它们导入到另一个数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4175396/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 08:16:13  来源:igfitidea点击:

Microsoft SQL Server: How to export data from a database and import them to another database?

sqlsql-server

提问by olidev

How can I export all of my rows in a table to sql script in Microsoft SQL Server 2005 and then import them to another database?

如何将表中的所有行导出到 Microsoft SQL Server 2005 中的 sql 脚本,然后将它们导入另一个数据库?

Thanks in advance

提前致谢

回答by El Kabong

If you moving it to another sql db you can right click the database you want and choose tasks -> generate scripts. That will launch a a wizard - follow along, choose the option to script all tables and data. Then execute that script in the new db(assuming that you've already created one with the same name)

如果您将其移动到另一个 sql db,您可以右键单击您想要的数据库并选择任务 -> 生成脚本。这将启动一个向导 - 继续,选择脚本所有表和数据的选项。然后在新数据库中执行该脚本(假设您已经创建了一个同名的)

回答by Matt

If you can't find a data import/export tool that will work in your particular circumstances, it's possible to write plain SQL SELECT queries that will generate SQL INSERT statements. In this way it's possible to "export" all your data to a script file that can be run against the destination database. It's kind of an ugly hack, but it's simple and it works if you don't have a lot of data to move. See my answer to this question for details: Export SQL Server 2005 query result to SQL INSERT statement?

如果您找不到适用于您的特定情况的数据导入/导出工具,则可以编写简单的 SQL SELECT 查询来生成 SQL INSERT 语句。通过这种方式,可以将所有数据“导出”到可以针对目标数据库运行的脚本文件中。这是一个丑陋的黑客,但它很简单,如果您没有大量数据要移动,它就可以工作。有关详细信息,请参阅我对此问题的回答:将 SQL Server 2005 查询结果导出到 SQL INSERT 语句?

Note that this method assumes that the destination table already exists. But it's pretty straightforward to generate table creation scripts, as J Cory's answerhas already shown.

请注意,此方法假定目标表已存在。但是生成表创建脚本非常简单,正如J Cory 的回答已经显示的那样。

回答by SqlRyan

There's a command line tool available to dump your data from particular tables into a SQL script that be executed against a different database:

有一个命令行工具可用于将特定表中的数据转储到针对不同数据库执行的 SQL 脚本中:

http://blog.sqlauthority.com/2007/11/16/sql-server-2005-generate-script-with-data-from-database-database-publishing-wizard/

http://blog.sqlauthority.com/2007/11/16/sql-server-2005-generate-script-with-data-from-database-database-publishing-wizard/

I don't believe SQL Management Studio Express supports data scripting (as your screenshot on J Cory's answer shows), but the full version does support that feature. In either case, the command line tool should accomplish what you need.

我不相信 SQL Management Studio Express 支持数据脚本(如 J Cory 回答中的屏幕截图所示),但完整版本确实支持该功能。在任何一种情况下,命令行工具都应该完成您的需求。