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;
分类:
SQL Server
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek “源神”启动!「GitHub 热点速览」
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器