博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在SQL Server中批量复制,导入和导出的技术
阅读量:2510 次
发布时间:2019-05-11

本文共 12003 字,大约阅读时间需要 40 分钟。

The process of importing or exporting large amounts of data into a SQL Server database, is referred to as bulk import and export respectively. Fortunately, we are provided with a plethora of native tools for managing these tasks incluing

将大量数据导入或导出到SQL Server数据库的过程分别称为批量导入和导出。 幸运的是,我们提供了许多本机工具来管理这些任务,包括

  • bcp utility

    bcp实用程序
  • Openrowset (Bulk) function Openrowset(批量)功能
  • SQL Server import and export wizard

    SQL Server导入和导出向导
  • Bulk insert statement 批量插入语句
  • Select into statement 选择进入声明

In this article, we’ll review various methods for accomplishing bulk data moving operations from one database to another.

在本文中,我们将介绍用于完成将大量数据从一个数据库移动到另一个数据库的各种方法。

SQL Server导入和导出向导 (SQL Server import and export wizard)

The SQL Server Import and export wizard provides a graphical user interface onto a SQL Server Integration Services (SSIS) package. Once created the package can be automated, to run on a schedule. It can be further configured and modified by using SQL Server Data Tools (SSDT)

SQL Server 导入和导出向导在SQL Server Integration Services(SSIS)程序包上提供了图形用户界面。 创建包后,可以将其自动化,以按计划运行。 可以使用SQL Server数据工具(SSDT)对其进行进一步配置和修改。

To begin, open the Import and export wizard, right-click a database and select the Tasks sub-menu -> Export data command:

首先,打开导入和导出向导,右键单击数据库,然后选择任务子菜单-> 导出数据命令:

  1. Connect to a source database via the Choose a data source step.

    通过“ 选择数据源”步骤连接到源数据库。

    Permissions: You will need the following permissions to for the source data source/instance

    权限:对于源数据源/实例,您需要具有以下权限

    1. read data from the database or file

      从数据库或文件中读取数据
    2. Insert permission on the msdb database to save the SSIS package插入权限以保存SSIS包

  2. Connect to a destination SQL Server database in the Choose a destination step.

    在“ 选择目标”步骤中连接到目标SQL Server数据库。

    Permissions: The following permissions are required for the destination instance:

    权限:目标实例需要以下权限:

    1. write data to the database or file

      将数据写入数据库或文件
    2. permissions to create a database

      创建数据库的权限
    3. if necessary, permission to create table

      如有必要,允许创建表

  3. Choose the Copy data from one or more tables or views option, In the Specify table copy or query step:

    在“ 指定表副本或查询”步骤中,选择“ 从一个或多个表或视图复制数据”选项:

  4. In the Select source tables and views step, choose the table(s) for which you want to export data from:

    在“ 选择源表和视图”步骤中,选择要从中导出数据的表:

  5. In the Save and run package step, choose the Run immediately option:

    在“ 保存并运行程序包”步骤中,选择“立即运行”选项:

  6. in the Complete the wizard step, Click Finish

    在“ 完成向导”步骤中,单击“ 完成”。

  7. Once completed, the following dialog will be displayed:

    完成后,将显示以下对话框:

Troubleshooting: The SQL Server import and export wizard will not distinguish identity columns from any other column type. This will lead to errors when inserting data into such columns

故障排除: SQL Server导入和导出向导不会将标识列与任何其他列类型区分开。 将数据插入此类列时,这将导致错误

Troubleshooting: The SQL Server import and export wizard also doesn’t process tables based on dependency order. An example might be loading a table, with a foreign key (the child), before the referencing table the parent, causing a foreign key constraint failure

疑难解答: SQL Server导入和导出向导也不会根据依赖关系顺序处理表。 一个示例可能是在父表的引用表之前加载带有外键(子项)的表,从而导致外键约束失败

批量复制 (Bulk copy )

bcp utility

bcp实用程序

The BCP utility is a console application, managed via the command line, that can build import/export data from a database to a file and visa versa

BCP实用程序是一个控制台应用程序,通过命令行进行管理,可以将导入/导出数据从数据库构建到文件,反之亦然。

For example, to export all TeamMemberIDs and Emails to the TeamMembers.txt  file from the TeamMemberEmail table in the QA database, run the following bcp command:

例如,要将所有TeamMemberID电子邮件QA数据库的TeamMemberEmail表导出到TeamMembers.txt文件,请运行以下bcp命令:

bcp QA.dbo.TeamMemberEmail out TeamMembers.txt –c -T 

bcp QA.dbo.TeamMember通过电子邮件发送TeamMembers.txt –c -T

The -c switch specifies that the utility is being used with character data and that the -T switch states that this process will use a trusted connection, the Windows login credentials of the user that is currently logged. If the -T option is not specified a username and password must be specified with the -U and –P options.

-c开关指定该实用程序与字符数据一起使用,并且-T开关指出此过程将使用受信任的连接,即当前登录的用户的Windows登录凭据。 如果未指定-T选项,则必须使用-U–P选项指定用户名和密码。

As would be expected, the destination table must exist prior to the import and the table must have the expected number and data types of columns to match the imported data.

可以预期,目标表必须在导入之前存在,并且该表必须具有预期的列数和数据类型以匹配导入的数据。

To insert data from the TeamMembers.txt  file into the NewSchema.dbo.TeamMemberEmail table use the following bcp command:

要将数据从TeamMembers.txt文件插入NewSchema.dbo.TeamMemberEmail表,请使用以下bcp命令:

bcp NewSchema.dbo.TeamMemberEmail in TeamMembers.txt -T –c

TeamMembers.txt中的 bcp NewSchema.dbo.TeamMemberEmail -T –c

Permissions: Select permissions are required on the source table

权限:在源表上需要选择权限

Bulk insert statement

批量插入语句

Another option for importing/exporting data between files and tables is the Bulk insert statement. The same restrictions and requirements that apply to BCP apply to Bulk insert as well including the requirement for a table to exist that matches the imported data

在文件和表之间导入/导出数据的另一种选择是Bulk insert语句。 适用于BCP的相同限制和要求也适用于批量插入,包括存在与导入的数据匹配的表的要求

First let’s specify location of the file from which to import the data and the destination database and table:

首先,让我们指定从中导入数据的文件的位置以及目标数据库和表:

BULK INSERT TestRemote.dbo.TeamMemberEmail FROM 'C:\TeamMembers.txt ';GO

Openrowset(Bulk) function

Openrowset(散装)功能

Openrowset(Bulk) is a T-SQL function that connects via an OLE DB data source to read data. It can access remoted data sources from a remote connection vs a linked server

Openrowset(Bulk)是通过OLE DB数据源连接以读取数据的T-SQL函数。 它可以从远程连接与链接服务器访问远程数据源

INSERT INTO AllEmails(Email)SELECT * FROM OPENROWSET(   BULK 'C:\TeamMembers.txt ',   SINGLE_BLOB) AS x;

The Openrowset(Bulk) function provides an alternative to accessing objects from a linked server and it is suitable for one-off entry of data from a remote source.

Openrowset(Bulk)函数提供了一种从链接服务器访问对象的替代方法,它适合一次性从远程源输入数据。

SELECT INTO

选择进入

The Into clause used, in combination with the Select statement, enables creating a new table based on the result set of the Select statement. For example, to copy the TeamMemberEmail table, on the same instance, in the default schema of the QA database, run the following query:

Into子句与Select语句结合使用,可以基于Select语句的结果集创建新表。 例如,要在QA数据库的默认架构中的同一实例上复制TeamMemberEmail表,请运行以下查询:

SELECT * INTO QA.dbo.TeamMemberEmail FROM Neptune.HumanResources.TeamMemberEmail;

Select into cannot be used to create a new table on a remote SQL Server instance, but a remote source can be included in the Select statement, if there is a link to the remote instance.

Select into不能用于在远程SQL Server实例上创建新表,但是如果存在指向远程实例的链接,则Select语句中可以包含远程源。

Any constraints, indexes, and triggers will not be transferred to the new table. Columns in the newly created table will not inherit the Identity property from the query output if the Select statement contains an aggregate function, a Join clause, or a Group by clause, and if an identity column is used in an expression, is used more than once, or is from a remote data source.

任何约束,索引和触发器都不会转移到新表中。 如果Select语句包含聚合函数, Join子句或Group by子句,并且在表达式中使用了identity列,则新创建的表中的列将不会从查询输出继承Identity属性 。一次,还是来自远程数据源。

使用ApexSQL脚本导出和导入数据 (Export and Import data by using ApexSQL Script)

Using , a SQL Server data and schema scripting and migration tool, you can make a  to export data, or both data and schemas, from a source instance and execute it on a destination instance to perform the import.

使用 (一种SQL Server数据和架构脚本和迁移工具),您可以使从源实例导出数据(或数据和架构),并在目标实例上执行以执行导入。

To script SQL Server database data using , follow these steps:

要使用编写SQL Server数据库数据的 ,请按照下列步骤操作:

  1. In the New project window, specify the server, data source and credentials to connect to a datasource:

    在“ 新建项目”窗口中,指定服务器,数据源和凭据以连接到数据源:

  2. ApexSQL Script can script objects, data or both. If both data and objects are scripted, in the Options tab, under the Structure section, assuming that a destination database exists, uncheck the Create database option and the Script use for database option.

    ApexSQL脚本可以编写对象,数据或两者的脚本。 如果数据和对象均已编写脚本,则在“ 选项”选项卡中“ 结构”部分下,假设存在目标数据库,请取消选中“ 创建数据库”选项和“ 脚本用于数据库”选项。

    If a tables containing constraints, indexes, or foreign keys exist, check the Names option to script the names of those objects:

    如果存在包含约束,索引或外键的表,请选中“ 名称”选项以编写这些对象的名称的脚本:

  3. ApexSQL Script handles identity fields. Under the Data section in the Options tab, check the Set identity insert on option. Under the Script rows as section, select the Insert option:

    ApexSQL脚本处理身份字段。 在“ 选项”选项卡的“ 数据”部分下,选中“ 设置身份插入方式”选项。 在“ 脚本行为”部分下,选择“ 插入”选项:

  4. If a destination database has a different schemas than the source database, in the Options tab, under the Owners in the script section, select the Exclude owners option to exclude schemas from the object names:

    如果目标数据库与源数据库具有不同的架构,请在“ 选项”选项中的“脚本中所有者”下,选择“ 排除所有者”选项以从对象名称中排除架构:

  5. Load button in the bottom-right corner of the 新建项目”窗口右下角的“ New project window 加载”按钮。
  6. In the Results grid, you can visualize the data to be scripted, at an aggregate level. In the Home tab, show the Data grid view. You can specify tables and even individual columns

    在“ 结果”网格中 ,您可以以聚合级别可视化要编写脚本的数据。 在“ 主页”选项卡中,显示“ 数据网格”视图。 您可以指定表,甚至可以指定单个列

  7. Clicking on the Structure button from the Home tab, switch to the Structure grid view and select specific tables (and/or views) to script:

    单击“ 主页”选项卡上的“ 结构”按钮,切换到“ 结构”网格视图,然后选择要编写脚本的特定表(和/或视图):

  8. Now that the scripting profile is complete, click the Script button from the Home tab to initiate the Script wizard:

    现在,脚本配置文件已完成,单击“ 主页”选项卡上的“ 脚本”按钮以启动“ 脚本”向导

  9. The script wizard will be shown. In the first step of the Script wizard, select the Structure and data scripting mode and click Next:

    将显示脚本向导。 在“ 脚本”向导的第一步中,选择“ 结构和数据”脚本模式,然后单击“ 下一步”

  10. In the Output type step, select the SQL output type and click Next:

    在“ 输出类型”步骤中,选择SQL输出类型,然后单击“ 下一步”

  11. Dependencies step 依赖关系”步骤中将显示并检查所有依赖对象
  12. In the final step, the SQL Script options step, select the Save the script to file option and click Create:

    在最后一步, SQL脚本选项步骤中,选择将脚本保存到文件选项,然后单击创建

  13. Open the newly created script in SQL Server Management Studio and execute it on the database of your choice

    在SQL Server Management Studio中打开新创建的脚本,然后在您选择的数据库上执行它

使用ApexSQL Data Diff复制数据 (Copy data by using ApexSQL Data Diff)

ApexSQL Data Diff is another useful tool for moving data. It is primarily designed as a comparison and synchronization tool, and as such it assumes that two tables, of similar structure, will exist in both databases.

ApexSQL Data Diff是另一个用于移动数据的有用工具。 它主要设计为比较和同步工具,因此它假定两个数据库中都将存在结构相似的两个表。

If a destination table already exists and it has the same structure as the source table, you can use ApexSQL Data Diff to migrate, import and export data. ApexSQL Data Diff is a  tool that can compare and synchronize database tables but also copy data from one table or tables to a destination database table or tables, as we’ll demonstrate now.

如果目标表已经存在并且与源表具有相同的结构,则可以使用ApexSQL Data Diff迁移,导入和导出数据。 ApexSQL Data Diff是一种工具,它可以比较和同步数据库表,还可以将数据从一个或多个表复制到目标数据库表或多个目标数据库,如我们现在将演示的那样。

To move data with , follow these steps:

要使用移动数据,请按照下列步骤操作:

  1. In the New project window, click the New button:

    在“ 新建项目”窗口中,单击“ 新建”按钮:

  2. Under the Data sources tab of the New project window, connect to the source and destination databases, and click the Compare button:

    在“ 新建项目”窗口的“ 数据源”选项卡下,连接到源数据库和目标数据库,然后单击“ 比较”按钮:

  3. In the Results grid, select specific tables even specific rows to copy:

    在“ 结果”网格中 ,选择要复制的特定表甚至特定行:

  4. Click the Synchronize button to initiate the Synchronization wizard:

    单击同步按钮以启动同步向导

  5. Go through the Synchronization wizard and under the Output options step, select the Create a synchronization script action along with the Save script to file option:

    经历“ 同步”向导,然后在“ 输出选项”步骤下,选择“ 创建同步脚本”操作以及“ 将脚本保存到文件”选项:

  6. In the last step of the Synchronization wizard preview the impact of the synchronization script and review any warnings and actions, before deciding to proceed:

    在决定继续之前,在“ 同步向导”的最后一步中预览同步脚本的影响并查看所有警告和操作:

  7. Click the Create script button, open it in SQL Server Management Studio and execute the script. Your data will have been successfully moved:

    单击创建脚本按钮,在SQL Server Management Studio中将其打开并执行脚本。 您的数据将被成功移动:

翻译自:

转载地址:http://ifiwd.baihongyu.com/

你可能感兴趣的文章
Alpha冲刺(10/10)
查看>>
数组Array的API2
查看>>
为什么 Redis 重启后没有正确恢复之前的内存数据
查看>>
No qualifying bean of type available问题修复
查看>>
第四周助教心得体会
查看>>
spfile
查看>>
Team Foundation Service更新:改善了导航和项目状态速查功能
查看>>
WordPress资源站点推荐
查看>>
Python性能鸡汤
查看>>
android Manifest.xml选项
查看>>
Cookie/Session机制具体解释
查看>>
ATMEGA16 IOport相关汇总
查看>>
JAVA基础-多线程
查看>>
面试题5:字符串替换空格
查看>>
[Codevs] 线段树练习5
查看>>
Amazon
查看>>
component-based scene model
查看>>
Echart输出图形
查看>>
hMailServer搭建简单邮件系统
查看>>
从零开始学习jQuery
查看>>