Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.databases.ms-sqlserver > #1145

Re: MSSMSE - Export results to CSV

From Erland Sommarskog <esquel@sommarskog.se>
Newsgroups comp.databases.ms-sqlserver
Subject Re: MSSMSE - Export results to CSV
Date 2012-06-21 23:40 +0200
Organization Erland Sommarskog
Message-ID <XnsA079F0DCCBB92Yazorman@127.0.0.1> (permalink)
References <5oudnZwNaMRT-37SnZ2dnUVZ8g-dnZ2d@bt.com>

Show all headers | View raw


Cathy (Cathy@Nospam.com) writes:
> I have tried saving the script and running it with osql
> (sorry should have mentioned earlier this is still an old SQL2000 server)
> But unfortunately the resultant CSV file seems to have a lot of blank
> spaces and wraps lines and just is not in the same CSV format as that
> exported in first step. 
> 
> I am therefore wondering if there is not a way in which I could add 
> something into the SELECT statement to save reults to CSV file and hope
> the results come out in the same format, 
 
You can use -h option to supress headers, -s to set the column separator and 
-w to control the width of the output. However, there will still be a lot of 
space. 

However, a better option for you may be BCP, at least as long as you 
don't want the header lines.

bcp "yourquerygoeshere" queryout outputfile.txt -c -t, -CRAW -T -S server

-T is for trusted connection. Use -U and -P if you use SQL Server 
authentication.

Beware that if there is comma in the data, bcp will not quote the value.


-- 
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Back to comp.databases.ms-sqlserver | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

MSSMSE - Export results to CSV "Cathy" <Cathy@Nospam.com> - 2012-06-21 19:24 +0100
  Re: MSSMSE - Export results to CSV Erland Sommarskog <esquel@sommarskog.se> - 2012-06-21 23:40 +0200
    Re: MSSMSE - Export results to CSV "Cathy" <Cathy@Nospam.com> - 2012-06-22 00:00 +0100
      Re: MSSMSE - Export results to CSV Erland Sommarskog <esquel@sommarskog.se> - 2012-06-22 11:33 +0200
        Re: MSSMSE - Export results to CSV "Cathy" <Cathy@Nospam.com> - 2012-06-22 18:48 +0100
          Re: MSSMSE - Export results to CSV Erland Sommarskog <esquel@sommarskog.se> - 2012-06-22 20:38 +0200
            Re: MSSMSE - Export results to CSV "Cathy" <Cathy@Nospam.com> - 2012-06-22 22:45 +0100
              Re: MSSMSE - Export results to CSV Erland Sommarskog <esquel@sommarskog.se> - 2012-06-23 11:28 +0200
                Re: MSSMSE - Export results to CSV "Cathy" <Cathy@Nospam.com> - 2012-06-23 23:49 +0100

csiph-web