惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
A
About on SuperTechFans
博客园 - Franky
Engineering at Meta
Engineering at Meta
Recent Announcements
Recent Announcements
云风的 BLOG
云风的 BLOG
B
Blog
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
Martin Fowler
Martin Fowler
Y
Y Combinator Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 叶小钗
Vercel News
Vercel News
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
Last Week in AI
Last Week in AI
腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
V
V2EX
G
Google Developers Blog

博客园 - hzwang

Administering your Windows Internal Database (MICROSOFT##SSEE) instance(转) VS 2008如何连接TFS 2010 learning F#(1):编程环境 learning F#: 开篇 Tip for using Fiddler on localhost (转载)ASP.NET MVC – Add CSS class Attribute I will write at least one blog every week 使用位逻辑运算来实现位向量 对象判等 C# 3.0 —— 扩展方法 读代码随笔(一):using语句的使用 visitor(访问者)模式 - hzwang - 博客园 抽象类与接口(转) 在 C# 中实现 Singleton (转) 设置VSS2005使支持通过Internet访问 VSS使用手册 ASP.NET Forms 身份验证控制流 Cookie使用总结 不断努力,不断学习
sql bcp Utility工具使用
hzwang · 2009-02-08 · via 博客园 - hzwang

bcp全称是Command Prompt Utilities,主要用来在数据库和数据文件中导入导出数据。

用法如下:

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]
  [-h "load hints"]         [-x generate xml format file]

现在我们从数据文件中导入数据到数据表中,我们需要一个格式文式,来定义从文本到数据库中字段的转化。

请注意,bcp里的参数设定是区别大小写的。

举例说明:bcp database.dbo.dataTable format nul –f C:\dataTable.fmt -x –c –T –S sql

database指定进行操作的数据

dbo指定数据库的所有者

dataTable指定数据库中的某个表

format nul –f C:\dataTable.fmt –x –c  指定导出的格式文件的存储位置和名称,-x说明的生xml格式的格式文件(推荐使用),-c指使用tab和换行来区分各个字段

-T 说明使用安全连接,采用windows认证,如果无法通过windows认证,可以使用sql认证,-U [username] –P [password]

-S 如果数据库不在本机,要指定你要连接的数据库服务器的名字

下面我们把数据表中的数据导出到数据文件中(数据库中的数据并不删除)。

举例说明:bcp database.dbo.dataTable out C:\output.txt –T –c –S sql

-S指明数据库服务器,database.dbo.dataTable指明数据库及表,-T使用windows认证

out C:\output.txt 指明这次操作做的是导出操作,把数据导出到C:\output.txt文件中

-c 产生以tab(\t)分隔列,换行(\r\n)分隔行的数据文件

现在我们可以再把数据文件按照格式文件的格式导入数据库中

举例说明:bcp database.dbo.dataTable in C:\output.txt –f C:\dataTable.fmt –e C:\error.txt –T –m 10 –S sql

in C:\output.txt指定这次操作是导入操作,且从output.txt文件导入

-e C:\error.txt如果在导入过程中发生错误,会将错误日志保存到这个文件中

-m 10指定允许发生错误的次数,当发生错误次数超出设定(在这里是10),这次导入操作次失败,回滚操作

参考资料:bcp UtilityCreating a Format File