简体   繁体   中英

Outputting SQL SERVER Query Results to CSV file

I'm using a basic query to gather data from a few joined tables, and I need to be able to export the data to a CSV (or text) file in order to be imported into Excel. The query format is:

SELECT  
Item1 as 'blah'  
FROM  
table1 JOIN table2  
WHERE Condition  
GROUP BY ...  
HAVING ....  

I have the proper output setup correctly through the query, so I'm only looking for a way to output it to a file. If it would be easier to use a stored procedure, then it would be no problem to throw that around the query. I'm just looking for something that can write the output to a file, WITHOUT using a third-party tool, as this needs to be moderately portable.

If you need more detail from the query, I can supply that (but it really is basic).

Use a BCP utility [MSDN]

That's what most commonly used.

If you have access to SSIS (through the Business Intelligence Development Studio), create a data flow task with an OLE DB Source going to a Flat File Destination. Or, you can go straight to Excel, if you don't want to worry about converting the delimited file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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