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 ApexSQL Script, a SQL Server data and schema scripting and migration tool, you can make a SQL script to export data, or both data and schemas, from a source instance and execute it on a destination instance to perform the import.

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

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

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

  1. ApexSQL Script ApexSQL脚本
  2. In the New project window, specify the server, data source and credentials to connect to a datasource:

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

  3. 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:

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

  4. 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脚本处理身份字段。 在“ 选项”选项卡的“ 数据”部分下,选中“ 设置身份插入方式”选项。 在“ 脚本行为”部分下,选择“ 插入”选项:

  5. 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:

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

  6. Load button in the bottom-right corner of the 新建项目”窗口右下角的“ New project window 加载”按钮。
  7. 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

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

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

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

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

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

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

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

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

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

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

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

  14. 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 SQL data compare 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是一种SQL数据比较工具,它可以比较和同步数据库表,还可以将数据从一个或多个表复制到目标数据库表或多个目标数据库,如我们现在将演示的那样。

To move data with ApexSQL Data Diff, follow these steps:

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

  1. ApexSQL Data Diff ApexSQL数据差异
  2. In the New project window, click the New button:

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

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

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

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

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

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

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

  6. 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:

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

  7. 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:

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

  8. 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中将其打开并执行脚本。 您的数据将被成功移动:

翻译自: https://www.sqlshack.com/techniques-to-bulk-copy-import-and-export-in-sql-server/

在SQL Server中批量复制,导入和导出的技术相关推荐

  1. 从TXT文本文档向Sql Server中批量导入数据

    因为工作的需要,近期在做数据的分析和数据的迁移.在做数据迁移的时候需要将原有的数据导入到新建的数据库中.本来这个单纯的数据导入导出是没有什么问题的,但是客户原有的数据全部都是存在.dat文件中的.所以 ...

  2. SQL Server中采用BULK INSERT实现大数据量文本文件批量导入

    SQL语句: BULK   INSERT   dbo.table        FROM   'e:/test.tbl '        WITH                (           ...

  3. 在SQL Server中的数据库之间复制表的六种不同方法

    In this article, you'll learn the key skills that you need to copy tables between SQL Server instanc ...

  4. 在SQL Server中导入和使用CSV文件

    介绍 (Introduction) CSV (comma separated values) is one of the most popular formats for datasets used ...

  5. 向SQL Server 中导入长文本

    向SQL Server 中导入长文本数据(如小说.报告)进行分析时,有一些细节需要注意,这里将流程做一个梳理: 文章目录 以每个段落作为一行进行导入 新建表 导入数据 数据源设置 映射设置 完成 截断 ...

  6. 使用T-SQL导入多个文件数据到SQL Server中一文的疑惑

    - 今天,由于毕业论文需要,我想弄数据转换,所以看了一看T-SQL,oracle实在没时间去学,好在SQL我还是很熟悉,所以打算看一看,写一些存储过程来完成任务. 我无意中在csdn上找到这样一篇文章 ...

  7. sql2008能否打开mysql数据库_mysql数据库数据能不能导入到sql server中

    点"测试"按钮确认你的链接是正确的. Press the "Test" button to ensure your connection settings ar ...

  8. 十步优化SQL Server中的数据访问

    故事开篇:你和你的团队经过不懈努力,终于使网站成功上线,刚开始时,注册用户较少,网站性能表现不错,但随着注册用户的增多,访问速度开始变慢,一些用户开始发来邮件表示抗议,事情变得越来越糟,为了留住用户, ...

  9. SQL Server中的数据层应用程序简介

    In this article, I'm going to introduce the data-tier applications in SQL Server. As the official do ...

最新文章

  1. redhat替换yum源时redhat.repo无法删除或禁用的问题
  2. 逻辑地址、线性地址、物理地址和虚拟地址初步认识
  3. JSP、EL和JSTL-学习笔记01【JSP基础语法】
  4. linux w 命令参数解释
  5. 利剑无意之scala小考核
  6. Please enable make sure that the ntop html/ directory is properly installed
  7. (8)Node.js 模块介绍
  8. APP界面的数据可视化设计临摹学习模板
  9. python删除列表内容_python删除列表内容
  10. E-Prime学习笔记
  11. 小程序 房租水电费记录管理_一点通房租物业收据打印软件下载
  12. [IOS]Presenting modal in iOS 13 fullscreen
  13. ih5语言叫html5,iH5最专业的H5制作工具
  14. 映像文件工具srec
  15. 从一维cutting问题看列生成算法
  16. 【数据库技术课程设计】 电信学院考研信息管理系统 +【Visual FoxPro】
  17. 暑期实习Day7---SpringMVC
  18. 8月9日华为发布了其自研的鸿蒙操作系统,华为正式发布自研操作系统鸿蒙
  19. 提问的力量(二):为什么提问不容易?
  20. Android PCM 播放全是噪音滋滋滋

热门文章

  1. 苹果企业账号使用注意事项
  2. WITH ROLLUP、WITH CUBE、GROUPING语句的应用
  3. 用原生javascript做的一个打地鼠的小游戏
  4. java多条件不定条件查询
  5. 1074: [SCOI2007]折纸origami - BZOJ
  6. Authentication-- 更改现有Sharepoint网站的认证方式,让其支持FBA:1.前期准备
  7. c#中的委托、事件、Func、Predicate、Observer设计模式以及其他
  8. 手写深浅拷贝(js)
  9. LeetCode(811)——子域名访问计数(JavaScript)
  10. JavaScript学习(五十)—hasOwnProperty属性和in关键字