bcp大容量复制实用工具

There are various methods available for bulk data operations.

有许多方法可用于批量数据操作。

  1. BCP utilityBCP实用程序
  2. BULK INSERT批量插入
  3. Using OPENROWSET使用OPENROWSET
  4. Import/Export wizard导入/导出向导

The BCP (Bulk Copy Program) utility is a command line that program that bulk-copies data between a SQL instance and a data file using a special format file. The BCP utility can be used to import large numbers of rows into SQL Server or export SQL Server data into files. The BCP data files don’t include any schema details or format information. Hence, it is recommended to create a format file to record the data format so in case of any failures, you can refer to the format file and better understand the data format to determine what may have gone wrong..

BCP(大容量复制程序)实用程序是一个命令行,该程序使用特殊格式的文件对SQL实例和数据文件之间的数据进行大容量复制。 BCP实用程序可用于将大量行导入SQL Server或将SQL Server数据导出到文件中。 BCP数据文件不包含任何架构详细信息或格式信息。 因此,建议创建一个格式文件来记录数据格式,以便在发生任何故障的情况下,您可以参考该格式文件并更好地了解该数据格式以确定可能出了什么问题。

We’ve been using the BCP tool for a long time, the reason being that it has a very low overhead, and works great for bulk exporting and importing of data. It is one of the most efficient ways to handle bulk import and export of data.

我们已经使用BCP工具很长时间了,原因是它的开销非常低,并且非常适合批量导出和导入数据。 这是处理批量导入和导出数据的最有效方法之一。

Article overview

文章概述

In this article, the BCP utility will be explained in detail. It covers the following topics:

在本文中,将详细介绍BCP实用程序。 它涵盖以下主题:

  1. BCP import and export command BCP导入和导出命令
  2. The format file 格式文件
  3. How to create format file 如何创建格式文件
  4. How to use the format file for data management 如何使用格式文件进行数据管理
  5. and more… 和更多…

Configuration

组态

SQL Server supports exporting of data from a SQL Server table and importing the same into another table. To run the BCP command in ssms, enable the xp_cmdshell server configuration parameter. This gives you the control to run extended stored procedures. Keep in mind, though, that this is not always a recommended option, since it exposes the SQL Server surface to potential threats from the outside world.

SQL Server支持从SQL Server表中导出数据并将其导入到另一个表中。 要在ssms中运行BCP命令,请启用xp_cmdshell服务器配置参数。 这使您可以控制运行扩展存储过程。 但是请记住,这并不总是推荐的选择,因为它使SQL Server表面暴露于来自外部世界的潜在威胁。

EXEC master..xp_cmdshell 'BCP ProdSQLShackDemo.dbo.SQLShackAuthor OUT f:\PowerSQL\ProdSQLShackDemo.txt -T -c'

BCP IN | OUT | QUERYOUT选项 (BCP IN | OUT | QUERYOUT options)

OUT: This option is used to export (or dump) all the records from a table into a data file. For example,

OUT:此选项用于将表中的所有记录导出(或转储)到数据文件中。 例如,

C:\WINDOWS\System32>BCP [AdventureWorks2014].[dbo].[SalesOrderDetail] out C:\SODetail_Out.txt -S hqdbt01\SQL2017 -T -c –b1000 –t,
  1. SQLShackDemoATC.dbo.SQLShackDemo – This is the name of the table that we intend to export. We can also use -d option to include the database name. SQLShackDemoATC.dbo.SQLShackDemo –这是我们要导出的表的名称。 我们还可以使用-d选项包括数据库名称。
  2. c :\SODetail_Out.txt – This is the output file where the data is dumped to c:\ SODetail_Out.txt –这是将数据转储到的输出文件
  3. -T – Trusted Windows authentication -T –可信的Windows身份验证
  4. -t, – define comma as the field separator -t,–将逗号定义为字段分隔符
  5. -w – Use wide width data format -w –使用宽数据格式
  6. -b1000 – Export the data in batches of 1000 rows -b1000 –批量导出1000行数据

IN: This option is used to import all the records to an existing table. This requires the table to be created before executing the BCP command.

IN:此选项用于将所有记录导入到现有表中。 这要求在执行BCP命令之前先创建表。

Let’s create a table to import the data, called SalesOrderDetailsIn using the following create statement

让我们使用以下create语句创建一个表以导入数据,称为SalesOrderDetailsIn

USE [AdventureWorks2014]
GO
CREATE TABLE [dbo].[SalesOrderDetailIn](
[SalesOrderID] [int] NOT NULL,
[SalesOrderDetailID] [int] IDENTITY(1,1) NOT NULL,
[CarrierTrackingNumber] [nvarchar](25) NULL,
[OrderQty] [smallint] NOT NULL,
[ProductID] [int] NOT NULL,
[SpecialOfferID] [int] NOT NULL,
[UnitPrice] [money] NOT NULL,
[UnitPriceDiscount] [money] NOT NULL,
[LineTotal] [numeric](38, 6) NOT NULL,
[rowguid] [uniqueidentifier] NOT NULL,
[ModifiedDate] [datetime] NOT NULL
) ON [PRIMARY]
GO

We can see that in the following BCP command that the IN keyword is used to import the data from the SODetails_Output.txt file.

我们可以看到,在下面的BCP命令中,IN关键字用于从SODetails_Output.txt文件导入数据。

C:\WINDOWS\System32>BCP [adventureworks2014].dbo.[SalesOrderDetailIn] in C:\SODetail_Output.txt -S hqdbt01\SQL2017 -T –c

BCP QUERYOUT

BCP查询

In the following example, we’re not going to export an entire table. Let’s look at a way to run a SQL statement to generate a data dump. The BCP export command is used in conjunction with the SELECT statement and the queryout option. Using queryout the SQL statement can be run on a defined connection and the data can be dumped into another file; the other switches are the same in this example as well.

在下面的示例中,我们将不会导出整个表。 让我们看一种运行SQL语句以生成数据转储的方法。 BCP导出命令与SELECT语句和queryout选项一起使用。 使用queryout可以在定义的连接上运行SQL语句,并且可以将数据转储到另一个文件中。 其他开关在此示例中也相同。

BCP "SELECT [SalesOrderID], [SalesOrderDetailID],[CarrierTrackingNumber] FROM [AdventureWorks2014].[dbo].[SalesOrderDetail]" queryout C:\SOQueryOut.txt -S hqdbt01\SQL2017 -T –cCREATE TABLE [dbo].[SalesOrderDetailQueryOut](
[SalesOrderID] [int] NOT NULL,
[SalesOrderDetailID] [int] IDENTITY(1,1) NOT NULL,
[CarrierTrackingNumber] [nvarchar](25) NULL
)C:\WINDOWS\System32>BCP [adventureworks2014].dbo.[SalesOrderDetailQueryOut] in C:\SOQueryOut.txt -S hqdbt01\SQL2017 -T -c

格式化文件 (Format files)

The BCP utility supports the use of a format file that contains the formatting details of each field in a file. The format file is used to provide all the required formatting details for bulk export and bulk import operations.

BCP实用程序支持使用格式文件,该文件包含文件中每个字段的格式详细信息。 格式文件用于为批量导出和批量导入操作提供所有必需的格式详细信息。

  1. It provides a flexible way to interpret the data. 它提供了一种灵活的方式来解释数据。
  2. It provides an interface to re-format the data during the export process. 它提供了在导出过程中重新格式化数据的界面。
  3. The format file eliminates the need of special programs for data import and export operations. 格式文件消除了用于数据导入和导出操作的特殊程序的需要。

SQL Server supports two kinds of format files: XML format files and non-XML format files. The non-XML format file is the original format supported by earlier versions of SQL Server.

SQL Server支持两种格式文件:XML格式文件和非XML格式文件。 非XML格式文件是SQL Server早期版本支持的原始格式。

In this section we shall walk through some of the concepts of the format file, look at how to create the format file, and other details.

在本节中,我们将逐步介绍格式文件的一些概念,研究如何创建格式文件以及其他细节。

Let’s Jump in and get started:

让我们开始吧:

CREATE TABLE SQLShackDemo(   EID smallint,   EName nvarchar(50) NULL,   EJOB nvarchar(50) NOT NULL   );
GO
INSERT INTO SQLShackDemo values(1,'Prashanth','DB Manager'),(2,'Bob','IT Director'),(3,'Scott','DevOp Eng');

创建非XML格式的文件 (Creating a Non-XML Format File)

A non-XML format file is an ordinary file with a well-defined structure, and contains metadata about the column such as storage type, prefix length, field length, and field terminator.

非XML格式的文件是具有良好定义的结构的普通文件,并且包含有关列的元数据,例如存储类型,前缀长度,字段长度和字段终止符。

The following BCP command is used to create format file:

以下BCP命令用于创建格式文件:

BCP TestDatabase.dbo. SQLShackDemo   format nul -c -f c:\BCPImport.fmt -t, -T
  1. Version 10.0 is the version number of the BCP utility. 版本10.0是BCP实用程序的版本号。
  2. Number of columns is the number of fields in the data file; in this case, it’s 3. 列数是数据文件中的字段数; 在这种情况下为3。
  3. The other fields in the format file describe the nature of the data fields. 格式文件中的其他字段描述了数据字段的性质。
  4. Field Order indicates the position of each field. 字段顺序指示每个字段的位置。
  5. Prefix Len is the length for the prefix characters. 前缀Len是前缀字符的长度。
  6. Data Len is the maximum allowed length for the data for the specific field. 数据长度是特定字段数据的最大允许长度。
  7. Terminator is the delimiter used to separate any two fields of the data. 终结符是用于分隔数据的任何两个字段的定界符。
  8. Column Order is the SQL Server table column order. 列顺序是SQL Server表的列顺序。
  9. Column Name is the name of the SQL Server table column. 列名是SQL Server表列的名称。
  10. Column Collation is the collation used to store the characters in the data file. 列排序规则是用于将字符存储在数据文件中的排序规则。

If -f is used with the format option, the specified format file is created for the specified table or view. To create an XML format file, specify the -x option.

如果-f与format选项一起使用,则将为指定的表或视图创建指定的格式文件。 要创建XML格式的文件,请指定-x选项。

Let’s see an example to skip ENAME column of a table and load the data into the SQLShackDemoSkip table using a format file. Before changing the format file, let’s create the SQLShackDemoSkip table and a data file with EID and EJOB data using the SQLShackDemo table.

让我们看一个示例,该示例跳过表的ENAME列,并使用格式文件将数据加载到SQLShackDemoSkip表中。 在更改格式文件之前,让我们使用SQLShackDemo表创建SQLShackDemoSkip表以及具有EID和EJOB数据的数据文件。

CREATE TABLE SQLShackDemoSkip   (   EID smallint,   EName nvarchar(50) NULL,   EJOB nvarchar(50) NOT NULL   );

Run the BCP command to generate the data file:

运行BCP命令以生成数据文件:

C:\WINDOWS\System32>BCP "select EID,EJOB from SQLShackDEMOATC.dbo.SQLShackDemo" queryout c:\SQLShackDemo3.txt -T -S HQDBT01\SQL2017 -c -t,

Following is the output of the data file:

以下是数据文件的输出:

Now, modify the format file, the first data field maps to EID, skips ENAME, and the third row maps the second data field of the data file to EJOB. The loading should ignore the ENAME column.

现在,修改格式文件,第一个数据字段映射到EID,跳过ENAME,第三行将数据文件的第二个数据字段映射到EJOB。 加载应忽略ENAME列。

To skip a table column, modify the format file definition of the corresponding row and set the values to 0 for all the related columns as shown below.

要跳过表列,请修改相应行的格式文件定义,并将所有相关列的值设置为0,如下所示。

Run the following BCP command to load the data into the SQLShackDemoSkip table.

运行以下BCP命令以将数据加载到SQLShackDemoSkip表中。

C:\WINDOWS\System32>BCP SQLShackDemoATC.dbo.SQLShackDemoSkip in C:\SQLShackDemo3.txt -f C:\BCPformat.fmt  -S hqdbt01\SQL2017 –T

The loading process excludes the second column of SQlShackDemoSkip.

加载过程不包括SQlShackDemoSkip的第二列。

SELECT * FROM SQLShackDemoSkip;

摘要 (Summary)

The BCP Utility, that’s very familiar to all SQL Server administrators, operates really well and is fast as well as efficient in terms of data import and export. Most of the options in the BCP command are case sensitive. It is recommended that we assume everything as case sensitive, so that we never run into troubles using the command.

所有SQL Server管理员都非常熟悉的BCP实用程序运行得很好,并且在数据导入和导出方面既快速又高效。 BCP命令中的大多数选项都区分大小写。 建议我们假设所有内容都区分大小写,以免使用该命令造成麻烦。

BCP is capable of exporting and importing really large chunks of data, but if you’re exporting/importing data in the range of tens of millions, it’s recommended to break it into smaller chunks. And this can be using the SELECT statement with the WHERE clause. When we’re bringing the data in, we need to be cautious of the defined constraints. Sometimes we need to delete the constraint and dump the data, and then re-add those deleted constraints.

BCP能够导出和导入非常大的数据块,但是如果要导出/导入数千万的数据,建议将其分成较小的块。 这可以与WHERE子句一起使用SELECT语句。 当我们引入数据时,我们需要注意定义的约束。 有时我们需要删除约束并转储数据,然后重新添加那些删除的约束。

目录 (Table of contents)

Getting started building applications using SQL Server DevOps Tools
Overview of SQLCMD utility in SQL Server
The BCP (Bulk Copy Program) command in action
Continuous Database Delivery (CD) using SQL Server Tools SqlPackage.exe
All about MSSQL-Scripter, the SQL Server cross-platform scripting Tool
Getting started with SQL Operations Studio (SOS); initial installation and configuration
开始使用SQL Server DevOps工具构建应用程序
SQL Server中SQLCMD实用工具概述
运行中的BCP(大容量复制程序)命令
使用SQL Server工具SqlPackage.exe的连续数据库传递(CD)
有关MSSQL-Scripter(SQL Server跨平台脚本工具)的全部信息
SQL Operations Studio(SOS)入门; 初始安装和配置

翻译自: https://www.sqlshack.com/bcp-bulk-copy-program-command-in-action/

bcp大容量复制实用工具

bcp大容量复制实用工具_运行中的BCP(大容量复制程序)命令相关推荐

  1. 昊鼎王五:Windows运行中的所有命令_Windows快捷命令_运行中的所有命令

    昊鼎王五:Windows运行中的所有命令_Windows快捷命令_"运行"中的所有命令 winver 检查Windows版本 wmimgmt.msc 打开Windows管理体系结构 ...

  2. kvm 虚拟机 实用工具笔记(方便查看ip 磁盘复制和修改文件等)

    kvm 实用工具总结: 1. libguestfs-tools 直接宿主机安装: yum -y install libguestfs-tools virt-df xxx 相当于df命令 virt-ca ...

  3. hadoop put命令的格式_工作中需熟练掌握的Hadoop命令

    作者信息 Elesdspline 目前从事NLP与知识图谱相关工作. 工作中需熟练掌握的Hadoop命令 导读 工作中经常要用到一些Hadoop命令,这里简单列举一下,熟悉基本的命令操作,工作效率事半 ...

  4. mysql主主复制半同步_mysql主从复制中的半同步复制

    实验mysql借助google提供的插件,完成半同步复制模型: 物理机依然是win7系统,虚拟机为centos7: 主节点:192.168.255.2 从节点:192.168.255.3 先配置为主从 ...

  5. python怎么在运行中查看执行状态_python,_python程序运行时 查看对象状态,python - phpStudy...

    python程序运行时 查看对象状态 不知大家有没有用过Rthymbox的python console. Rthymbox可以在运行的时候,让它的 交互式命令行 对程序进行控制. 比如说可以调用pla ...

  6. lisp代码编写地物符号_工程图中标注序号的LISP程序

    工程图中标注序号的LISP程序 (2007-04-03 17:11:45) 在工程设计工作中,经常要对设备.零件等标注序号.根据标准化要求,在标注点处要加上实心小圆点,然后引出直线 ;在另一端画出一个 ...

  7. sql 缓冲池_运行中SQL Server缓冲池

    sql 缓冲池 SQL Server retrieves data from two areas; memory and disk. As disk operations are more expen ...

  8. windows复制文件路径_如何在Windows 10上复制文件的完整路径

    windows复制文件路径 Sometimes, it's handy to copy the full path of a file or folder in Windows 10 to the c ...

  9. numpy的深复制与浅复制的区别_浅谈数据备份与复制对于企业用户的区别

    随着服务器海量数据的不断增长,数据的体积变得越来越庞大.同时,各种数据的安全性和重要程度也越来越被人们所重视.对数据备份的认同涉及到两个主要问题,一是为什么要备份,二是为什么要选择磁带作为备份的介质. ...

最新文章

  1. js离焦事件_JavaScript中的事件
  2. ITK:读未知的图像类型
  3. redis类型[string 、list 、 set 、sorted set 、hash]
  4. JFlow CCFlow工作流引擎北京培训邀请函
  5. 《南溪的目标检测学习笔记》——数据集制作(legacy)
  6. 某 iOS 零点击 0day 漏洞已存在8年之久且正遭利用?苹果称正在调查并将推出补丁...
  7. 校门外的树和memset
  8. redis查询冒号下_实战|还在用单机版?教你用Docker+Redis搭建主从复制多实例
  9. Photoshop安装:详细安装步骤
  10. Python自动化填写问卷星问卷
  11. Flink从1.7到1.14版本升级汇总
  12. Vim插件之python-mode
  13. Perfmon - Windows 自带系统监控工具
  14. 显卡系列和种类你了解多少?AMD、NVIDIA显卡盘点
  15. 商务与经济统计阅读笔记3
  16. 什么样的CTA策略组合最合理?
  17. Xshell 使用删除键乱码问题
  18. 开源Tizen系统视频泄露 酷似MeeGo及Bada
  19. 怎样找回我的世界服务器密码,我的世界服务器
  20. Google软件工程(续)

热门文章

  1. c语言编程指法输入,C语言 课件 第一章引论.pdf
  2. python 类变量 none 内存泄露_Python中使用自定义类class作为海量数据结构时内存占用巨大的问题...
  3. 分奇偶数c语言_小白学写字(C语言)
  4. 探索性测试 之 极速测试
  5. 新手安装Ubuntu操作系统
  6. flask结合令牌桶算法实现上传和下载速度限制
  7. 房子过户给子女哪种方式最合适?买卖?赠与?继承?不看就亏大了!
  8. Knockout v3.4.0 中文版教程-14-控制文本内容和外观-style绑定
  9. UVALive 6909 Kevin's Problem 数学排列组合
  10. HDU 1166 敌兵布阵 【线段树-点修改--计算区间和】