简体   繁体   English

SQL Server:使用批量导出实用程序将数据导出到csv文件时删除换行符

[英]SQL Server: removing line breaks when exporting data to csv file using Bulk export utility

I have a table which I need to export to a csv file. 我有一个表,我需要导出到csv文件。 This table has an xml field which can have line breaks that I must remove. 这个表有一个xml字段,可以有我必须删除的换行符。 I am using the bcp utility to export data from Sql Server to the csv file. 我正在使用bcp实用程序将数据从Sql Server导出到csv文件。

This is the strucutre of the table: 这是表格的结构:

ID int
CODE varchar(20)
XML_DATA xml

This is the command I am using: 这是我使用的命令:

bcp "SELECT ID, CODE, replace(convert(nvarchar(max), XML_DATA), CHAR(13)+CHAR(10), ' ') as XML_DATA FROM MYDB.dbo.MyTable" queryout C:\output.csv -c -t0x1F -T -S "10.10.10.28"

For some reasons the output file still contains the line breaks. 由于某些原因,输出文件仍包含换行符。 But if I run the same query on SQL Server for a record which has a line break, the output is correct (line breaks removed): 但是,如果我在SQL Server上为具有换行符的记录运行相同的查询,则输出正确(删除换行符):

SELECT
    ID, 
    CODE, 
    replace(convert(nvarchar(max), XML_DATA), CHAR(13)+CHAR(10), ' ') as XML_DATA
FROM MYDB.dbo.MyTable
WHERE ID = 1099; -- record with a line break in the XML_DATA field

What am I missing here? 我在这里错过了什么?

Solved replacing this line 解决了这条线的问题

replace(convert(nvarchar(max), XML_DATA), CHAR(13)+CHAR(10), ' ') as XML_DATA

with this 有了这个

replace(replace(convert(nvarchar(max), XML_DATA), CHAR(13), ' '), CHAR(10), ' ') as XML_DATA

Also I would like to point out that Sql Server Management Studio by default does not include CLRF characters on copy/paste from a query result, you must explicitly enable an option to preserve CLRF characters and restart SQL Server: preverse CLRF on Sql Server Management Studio query result . 另外我想指出,默认情况下,Sql Server Management Studio在查询结果的复制/粘贴时不包含CLRF字符,您必须显式启用选项以保留CLRF字符并重新启动SQL Server: 在Sql Server Management Studio上预反转CLRF查询结果 That was giving me some confusion ^_^ 那给了我一些困惑^ _ ^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM