SQL Server BCP:如何在所有字段周围加引号?
我有这个 BCP 命令:
'bcp DBName..vieter out c:\test003.txt -c -T /t"\",\"" -S SERVER'
我得到的输出 CSV 不会在字段名称周围加上引号,而是将其放在逗号周围!如何让 /t"\",\""
在所有字段周围加上引号。
谢谢大家
I have this BCP command:
'bcp DBName..vieter out c:\test003.txt -c -T /t"\",\"" -S SERVER'
The output CSV I get does not put quotes around the field names, instead it puts it around the commas! How can I get the /t"\",\""
to put quotes around all fields.
Thanks all
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
除了字段终止符之外设置行终止符应该可以解决这个问题
这可能会起作用,但是错过了第一行的第一个字段的前导“,也许最后一行的最后一个字段 - 我不确定,只是猜测,这里没有服务器!
或者尝试使用 QUOTENAME 来换行文本字段(您也可以换行数字,但这通常不是必需的。)
Setting the row terminator in addition to the field terminator should do the trick
This will likely work, but miss off the leading " for the first field of the first line, and perhaps the last field of the last line - I'm not sure, just guessing really, no server here!
or try using QUOTENAME to wrap text fields (you could also wrap numbers, but that isn't normally required.)
您需要使用 CHAR(34) 进行引用。此页面有更多详细信息: http://www.sqlteam.com/forums/topic .asp?TOPIC_ID=153000
You need to use CHAR(34) for the quote. This page has more details: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=153000
或者,如果您适合基于 Powershell 的脚本,您可以尝试使用下面的代码,它会自动引用。
Alternatively, if you are fine for Powershell based script, you can try with below code, which does automatic quoting.
这将在每个字段之前和之后添加引号(包括第一个和最后一个字段)。
This will put quotes before and after each field (including the first and the last).
这是我使用的命令列表。
Here are the list of commands i used .
我想您的目标是通过使用唯一标识符来清楚地分隔字段值,以便导入过程不会出现问题。
我遇到了同样的问题,发现这个解决方法很有用:使用不寻常的字段终止符,例如
|
甚至字符串/#/
可以非常独特,不应该混淆你的字符串内容。您还可以使用十六进制值(有限,请参阅 https://learn.microsoft.com/en-us/sql/tools/bcp-utility?view=sql-server-2017)导出
导入
I guess your goal was to clearly seperate field values by using an unique identifier so that import procedure doesn't have an issue.
I had same issue and found this workaroud useful: Using an unusual field terminator, for example
|
or even a string/#/
can be very unique and shouldn't mess with your string content. You also can HEX-Values (limited, see https://learn.microsoft.com/en-us/sql/tools/bcp-utility?view=sql-server-2017)export
import
删除前导引号的实际可行答案是:
A)使用 bcp 生成格式文件:
B)编辑该文件以手动将字段 1 复制到上面的字段 0,作为第一个字段,设置 Max_Length=1 并删除分隔符并在 field1 中添加一个引号
该技巧有效,因为您添加一个字段(文件的接口)来检测第一个分隔符,这会导致始终为空值,但不会添加一行(文件的接口)查询输出)。
The actual workable answer, that removes the leading quote, is to :
A) generate format file with bcp :
B) edit that file to manually copy field 1 to field 0 above, as the first field, set Max_Length=1 and remove the separator and one quot the was in field1
The trick works, as you are adding a field (interface to the file) to detect the first seprator, which results in an always null-value, but not add a row (interface for the query output).