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

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - Jason's WMI SQL Related Blog

关于SQL SERVER的内存使用的问题 使用Raid提高SQL的性能和可用性 SQL Server BCP 的数据导入导出 - Jason's WMI SQL Related Blog 用脚本查看某库中每个表大小 用脚本查看Server上每个库的大小 Easy way to estimate a table size in the future How to determine which version of SQL Server 2000 is running modify数据时临时暂停触发器的作用 关于MSBA和Firewall 简单的ASP.net查询数据库脚本 关于DTS中全局变量数据类型date Log Shipping实现 如何使用SQL 2000的DTS自动从FTP服务器下载文件 SQL Server数据库备份还原 SQl Server中修改数据库的备份模型后要完全备份一次数据库 如何在SQL SErver2000中恢复Master数据库 SQLSERVER 管理DTS包 使用WMI重启不能用3389登录的服务器 如何使用T-SQL来给系统增加计划任务,
关于如何在BCP和Bulk Insert中使用 文本限定符
Jason's WMI SQL Related Blog · 2005-11-30 · via 博客园 - Jason's WMI SQL Related Blog
关于如何在BCP和Bulk Insert中使用 文本限定符

如果你使用SQL Server的导入导出工具,你会在选择格式的时候有一个 文本限定符的选项,选择使用“ 或者‘ 把文本的内容引起来,但是在BCP和Bulk Insert中就找不到类似的参数和选项;

这次数据同步中,由于不考虑使用SQL之间直接传送数据,所以使用了文本文件作为中转,直接使用了BCP.exe,但是其中有些乱码的内容,就出现了问题; 再次导入的时候就会丢失一部分数据。

但是使用SQL自己的导出工具wizard,导出的文本的内容会用”引起来,再次用同样的办法导入的时候,数据则不会丢失;

在网上搜了好多bcp+"text qualifier",发现有好多人问题,但是大部分还是使用了其它的办法来导出数据;最终还是找到一个解决办法:修改格式文件fmt,

如果先生成一个格式文件,大概会是下面样子:

7.0 

1 SQLCHAR 0 11 "," 1 au_id "" 
2 SQLCHAR 0 40 "," 2 au_lname "" 
3 SQLCHAR 0 20 "," 3 au_fname "" 
4 SQLCHAR 0 12 "," 4 phone "" 
5 SQLCHAR 0 40 "," 5 address "" 
6 SQLCHAR 0 20 "," 6 city "" 
7 SQLCHAR 0 2 "," 7 state "" 
8 SQLCHAR 0 5 "," 8 zip "" 
9 SQLCHAR 0 1 "\r\n" 9 contract "" 

但是如果要加上”,则需要改成这个样子:

8.0 

1 SQLCHAR 0 11 ",\"" 1 au_id "" 
2 SQLCHAR 0 40 "\",\"" 2 au_lname "" 
3 SQLCHAR 0 20 "\",\"" 3 au_fname "" 
4 SQLCHAR 0 12 "\",\"" 4 phone "" 
5 SQLCHAR 0 40 "\",\"" 5 address "" 
6 SQLCHAR 0 20 "\",\"" 6 city "" 
7 SQLCHAR 0 2 "\",\"" 7 state "" 
8 SQLCHAR 0 5 "\",\"" 8 zip "" 
9 SQLCHAR 0 1 "\"\r\n" 9 contract "" 

这样出来的结果,实际上是把,改成了\",\" ,其中\是作为转义符出现的;也就是在,号的左右都加了";但是注意第一列和其它的不同;结果是第一列没有使用“”引起。

如果你需要将第一列也使用“引起来的话,需要在你的数据前再增加一列 null firstquote, 内容为空,实际上在你真正的第一列前面增加一个前置的” ;具体的办法请参见下面的内容,我没有用到,所以也没有深入搞,大家可以参考一下:

You need to use a format file for this. Using the pubs..authors table as an example,
we'll bcp out of a view that looks like this:

use pubs go create view authors_csv as select null first_quote, * from authors

Note that we are including a dummy column called first_quote that just returns NULL.
It's just a little trick to get the leading quote on the first column.

The format file looks like this:

8.0

10

 1 SQLCHAR 0 0 "\"" 1 first_quote ""

2 SQLCHAR 0 11 "\",\"" 2 au_id ""

3 SQLCHAR 0 40 "\",\"" 3 au_lname ""

4 SQLCHAR 0 20 "\",\"" 4 au_fname ""

5 SQLCHAR 0 12 "\",\"" 5 phone ""

6 SQLCHAR 0 40 "\",\"" 6 address ""

7 SQLCHAR 0 20 "\",\"" 7 city ""

8 SQLCHAR 0 2 "\",\"" 8 state ""

9 SQLCHAR 0 5 "\",\"" 9 zip ""

10 SQLCHAR 0 1 "\"\r\n" 10 contract ""

That dummy column is also in the format file to get the leading quote on au_id.
Here's the command line:

bcp pubs..authors_csv out authors_csv.dat -fauthors_csv.bcp -Slindaw\ml_tg -T

If you only need to enclose some of the columns in quotes, you can remove the
extraneous one from the format file.

Another alternative is to create a view that concatenates the quotes to the
column values.

Linda