20.1 SQL Server 批量复制程序(BCP)

SQL Server BCP

BCP工具简介

BCP(Bulk Copy Program)代表批量复制程序。BCP是一种命令行工具,它使用批量复制程序API,可以在SQL Server实例和文件之间进行批量复制数据。

通常,BCP可以:

  • 将表中的数据批量导出到数据文件
  • 把查询结果批量导出到数据文件
  • 从数据文件导入数据到表
  • 生成格式化文件

BCP程序直接通过命令行启动。

bcp

查看版本信息:

bcp -v

要查看bcp的所有选项:

bcp -?

输出:

usage: bcp {dbtable | query} {in | out | queryout | format} datafile
  [-m maxerrors]            [-f formatfile]          [-e errfile]
  [-F firstrow]             [-L lastrow]             [-b batchsize]
  [-n native type]          [-c character type]      [-w wide character type]
  [-N keep non-text native] [-V file format version] [-q quoted identifier]
  [-C code page specifier]  [-t field terminator]    [-r row terminator]
  [-i inputfile]            [-o outfile]             [-a packetsize]
  [-S server name]          [-U username]            [-P password]
  [-T trusted connection]   [-v version]             [-R regional enable]
  [-k keep null values]     [-E keep identity values][-G Azure Active Directory Authentication]
  [-h "load hints"]         [-x generate xml format file]
  [-d database name]        [-K application intent]  [-l login timeout]

使用SQL Server bcp将数据从表导出到文件

将数据从表格导出到文件:

bcp database_name.schema_name.table_name out "path_to_file" -c -U user_name -P password

其中:

  • bcp: bcp命令
  • database_name:数据库名称
  • schema_name: 架构名称
  • table_name: 表名称
  • path_to_file:输出文件路径
  • -c:指定导出字符编码格式
  • -U user_name指定连接到数据库的用户。此用户需要对要导出的表具有SELECT权限。
  • -P password指定用户的密码。

如果希望bcp程序使用Windows集成安全系统通过受信任的连接连接到SQL Server,可以使用-T选项而不是指定用户名和密码:

bcp database_name.schema_name.table_name out "path_to_file" -c -T

例如,以下命令将产品(products)表的数据从Bikestores示例数据库的production架构导出到文件d:\data\products.txt

bcp kikestores.production.products out "d:\data\products.txt" -c -U [username] -P [password]

输出:

Starting copy...

321 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total     : 15     Average : (21400.00 rows per sec.)

注意,需要在D:驱动器上有data文件夹才能成功运行命令。

使用SQL Server bcp将查询结果导出到文件

bcp "query" queryout "path_to_file" -w -U [username] -P [password]

命令中,在双引号中加上指定查询语句,并使用queryout选项。

例如,以下命令从查询中导出数据,该查询从production.products中查询产品名称和标价:

bcp "select product_name, list_price from bikestores.production.products where model_year=2017" queryout "d:\data\products.txt" -w -U sa -P Abcd1234
Starting copy...

85 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total     : 16     Average : (5312.50 rows per sec.)

使用SQL Server bcp将数据从文件导入表

首先,创建一个带有People表的新的人力资源数据库:

CREATE DATABASE hr;
GO

USE hr;

CREATE TABLE people (
  id int IDENTITY PRIMARY KEY,
  first_name varchar(50) NOT NULL,
  last_name varchar(50) NOT NULL
);

INSERT INTO people (first_name, last_name)
  VALUES ('John', 'Doe'), ('Jane', 'Doe');

SELECT * FROM people;

然后,将人员表中的数据导出到文件中:

bcp hr.dbo.people OUT d:\data\people.bcp -T -c
Starting copy...

2 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total     : 1      Average : (2000.00 rows per sec.)

然后清空People表:

TRUNCATE TABLE people;

然后,从d:\data\people.bcp导入数据到人员表:

bcp hr.dbo.people IN d:\data\people.bcp -T -c
Starting copy...

2 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total     : 16     Average : (125.00 rows per sec.)

最后验证一下导入的数据

SELECT * FROM people;

posted @ 2023-01-31 16:34  平元兄  阅读(365)  评论(0编辑  收藏  举报