简体   繁体   English

查询结果输出到csv文件

[英]Query result output to csv file

I'm using the following query to save all the result in .CSV file returned by SQL query. 我正在使用以下查询将所有结果保存在SQL查询返回的.CSV文件中。

EXEC  master..xp_cmdshell 'SQLCMD -S . -E -Q "SELECT TOP 10
    customercode, customername, businessdescription, ctypecode
FROM ArchivalDB.dbo.customer" -s"," -b -o d:\data\myoutput1.csv', no_output

It works but, if any of my column contact ',' than the output of csv file gets disturbed. 它可以工作,但是,如果我的任何列联系人为“”,那么csv文件的输出就会受到干扰。

I know that has to do something with the -s parameter. 我知道这与-s参数有关。

Can anyone please help me in this matter. 任何人都可以在这件事上帮助我。

Thanks 谢谢

If you are ok removing comma with space or any other delimiter, then change your sql query to 如果可以删除带空格或其他定界符的comma ,则将SQL查询更改为

SELECT TOP 10
    replace(customercode,',',' '), 
    replace(customername,',',' '), 
    replace(businessdescription,',',' '), 
    replace(ctypecode,',',' ')
FROM ArchivalDB.dbo.customer

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

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