简体   繁体   English

如何获得CSV格式的SQL查询结果?

[英]How can I get a SQL query result in CSV format?

I have an SQL query that returns some rows. 我有一个SQL查询,返回一些行。 How can I change it so that the output of the query is in CSV format? 如何更改它,以使查询的输出为CSV格式?

I have found a way to make the result in XML format, but because of the nature of XML the result is too large to be useful. 我找到了一种以XML格式生成结果的方法,但是由于XML的性质,结果太大而无法使用。

The query is very large (and it's not really important), so I wont post it here. 该查询非常大(并不是很重要),因此我不会在此处发布。 Here is a link to it. 这是它的链接。 Link 链接

I don't want the SQL query to save the result as a CSV file.

YES

try this 尝试这个

DECLARE  @query nvarchar(max)

select @query = STUFF(  (SELECT N',' + cast( id as nvarchar)  
from TableX 
--some WHERE clause here as well

FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)')  ,1,1,'')

select  @query

NO variable : 没有变量:

 select   STUFF(  (SELECT N',' + cast( id as nvarchar)  
 from TableX 
 FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)')  ,1,1,'')

Like this: 像这样:

select field1
, '","'
, field2
, '","'
etc

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

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