简体   繁体   English

将带转义引号的SQL数据库批量导出到CSV

[英]Batch export SQL database to CSV with escaping quotes

I'm looking for a way to batch Export a SQL Server table to a csv file. 我正在寻找一种将SQL Server表批量导出到csv文件的方法。

There are some solutions using sqlcmd or bcp, but so far I found none which properly escapes quotes, commas or line-breaks. 有一些使用sqlcmd或bcp的解决方案,但是到目前为止,我发现没有一种可以正确地避免使用引号,逗号或换行符。

For example this command creates a nice csv but does ignore quotes and commas which renders the csv file unusable: 例如,以下命令创建了一个不错的csv,但是却忽略了引号和逗号,从而使csv文件不可用:

bcp MyDatabase..MyTable out c:\test.csv -c -T -t, -r\n -S MYPC

From my sample data of four rows each containing some other special character this would create a file like this: 从我的四行样本数据中,每行包含其他一些特殊字符,这将创建一个文件,如下所示:

1,contains " quote
2,contains , comma
3,contains ; semi
4,contains ' single quote

Due to the quotes and the comma this is not importable by other programs. 由于引号和逗号,其他程序无法导入。 Of course I could change the separator to tab or the pipe symbol, but this does not fix the real problem: Whatever the separator is, if it exists in the data it will render the export file unusable. 当然,我可以将分隔符更改为tab或管道符号,但这不能解决真正的问题:无论分隔符是什么,如果数据中存在分隔符,它将导致导出文件不可用。

So how do I bulk export data in a batch to a working csv file using standard SQL tools like BCP, sqlcmd or similar? 那么,如何使用BCP,sqlcmd或类似的标准SQL工具将批量数据批量导出到工作的csv文件中?

Using quotename should properly escape quotes (but it's limited to max 128 chars, no line-breaks): 使用quotename应该可以正确地转义引号(但是最大为128个字符,没有换行符):

BCP " select quotename(quotedCol,CHAR(34)),quotename(secondCol,CHAR(34))from 
testdb.dbo.table_1" queryout temp.csv -c -T -S. -t","

given values this is "between quotes" and def it produces: 给定值, this is "between quotes"def产生:
"this is ""between quotes""","def" which is I believe properly quoted/escaped according to csv quidelines. 我相信根据CSV quidelines "this is ""between quotes""","def"被正确地引用/转义了。

Source: http://social.technet.microsoft.com/wiki/contents/articles/4666.sql-server-bcp-utility-experts-guide.aspx#Use_Text_Qualifier_on_BCP_Output 来源: http//social.technet.microsoft.com/wiki/contents/articles/4666.sql-server-bcp-utility-experts-guide.aspx#Use_Text_Qualifier_on_BCP_Output

Either make it fixed width or manually add quote delimiters. 使其固定宽度或手动添加引号定界符。 Both of these can be achieved with a view 两者都可以通过以下方式实现

For example your view would be 例如,您的观点是

SELECT C1, '"' + REPLACE(C2,'"','\"') + '"' As C2 FROM YourTable

Then you select from this view in your BCP and C2 will be quote delimited, and quotes in the data will be escaped with \\ (mostly) 然后在BCP中从此视图中选择,C2将用引号定界,并且数据中的引号将用\\进行转义(大多数情况下)

To make it fixed width is just another string expression that concatenates the fields with appropriate padding. 为了使其具有固定的宽度,这只是另一个字符串表达式,它使用适当的填充符将字段连接在一起。

You can use a query in BCP but I'm not sure how you escape the quotes (!) No matter what you do those quotes are a pain. 您可以在BCP中使用查询,但是我不确定您如何转义引号(!),无论您做什么,这些引号都是一种痛苦。

I found a solution which properly encodes csv files in another Stackoverflow answer by Iain Elder : 我找到了一种解决方案,可以 Iain Elder的 另一个Stackoverflow答案中正确编码csv文件:

He uses PowerShell to Export proper csv: 他使用PowerShell导出适当的csv:

Import-Module -Name SQLPS
$cd = Get-Location
Invoke-Sqlcmd -Query "SELECT * FROM DimDate;" `
              -Database AdventureWorksDW2012 `
              -Server localhost |
Export-Csv -NoTypeInformation `
           -Path "$cd\DimDate.csv" `
           -Encoding UTF8

His solution properly encodes delimiters, line breaks, quotes and works with long content, too. 他的解决方案可以正确地编码定界符,换行符,引号,并且可以长时间使用。

I still find it strange that no other export seems to properly support csv. 我仍然感到奇怪,似乎没有其他导出可以正确支持csv。 It's not that complicated. 那么复杂。

Does it have to be csv? 一定要是csv吗? I usually prefer txt files to avoid this kind of problem. 我通常更喜欢使用txt文件来避免这种问题。

     bcp MyDatabase..MyTable out c:\test.csv -c -T , -r\n -S MYPC

If you have the possibility to use other delimiters try 如果您可以使用其他定界符,请尝试

     bcp MyDatabase..MyTable out c:\test.csv -c -t| -T, -r\n -S MYPC   

Other ways to achieve well formed csv are decribed here: https://www.simple-talk.com/sql/database-administration/creating-csv-files-using-bcp-and-stored-procedures/ 此处介绍了实现格式正确的csv的其他方法: https ://www.simple-talk.com/sql/database-administration/creating-csv-files-using-bcp-and-stored-procedures/

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

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