x

Output query results to csv files

I have a query that returns 4 select result sets. Is there a way to output the results into 4 csv files from the query without having to click on each result set and "save result as" and selecting CSV file? Please advice if there is a better way of producing the csv files. thanks, Doug

more ▼

asked Dec 14, 2010 at 10:16 AM in Default

avatar image

Doug
43 4 4 6

(comments are locked)
10|1200 characters needed characters left

4 answers: sort voted first

That's something you can do in my SQL Server IDE - right click a result set and say 'save all to CSV'. Not sure how you would do that in SSMS though. Funnily enough, I don't use it that much :)

more ▼

answered Dec 14, 2010 at 10:35 AM

avatar image

Matt Whitfield ♦♦
29.5k 62 66 88

(comments are locked)
10|1200 characters needed characters left

BCP using queryout

more ▼

answered Dec 14, 2010 at 10:36 AM

avatar image

ozamora
1.4k 3 19 7

(comments are locked)
10|1200 characters needed characters left

Is BCP using Queryout is only in windows command line? I try to run it in sql management studio and I get a incorrect syntax. I query the results into a temp table and ran the following script

BCP " Select * from #Result1 Order by [Territory] ,[selltoCustCountryCode] ,Year ,CustCount " queryout Test.csv -c -T

more ▼

answered Dec 14, 2010 at 11:15 AM

avatar image

Doug
43 4 4 6

Yes. You can execute using xp_cmdshell if the feature is enabled for the instance.

Dec 14, 2010 at 11:19 AM ozamora
(comments are locked)
10|1200 characters needed characters left

I've enabled xp_cmdshell and try to run the below statement but I'm coming up with an error: Msg 155, Level 15, State 1, Line 3 'BCP' is not a recognized ODBC date/time extension option.

This is new territory for me, can you please review the code below and let me know where I error out?

EXEC xp_cmdshell { BCP ' Select * from #Result1 Order by [Territory] ,[selltoCustCountryCode] ,Year ,CustCount ' queryout Test.csv -c -T }

more ▼

answered Dec 14, 2010 at 11:44 AM

avatar image

Doug
43 4 4 6

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x2031
x1170
x1090
x446

asked: Dec 14, 2010 at 10:16 AM

Seen: 2697 times

Last Updated: Dec 14, 2010 at 10:16 AM

Copyright 2018 Redgate Software. Privacy Policy