Path: csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Erland Sommarskog Newsgroups: comp.databases.ms-sqlserver Subject: Re: MSSMSE - Export results to CSV Date: Thu, 21 Jun 2012 23:40:39 +0200 Organization: Erland Sommarskog Lines: 33 Message-ID: References: <5oudnZwNaMRT-37SnZ2dnUVZ8g-dnZ2d@bt.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit Injection-Info: mx04.eternal-september.org; posting-host="nBFDv6s1VJQDuF1w6hpX2A"; logging-data="31598"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19qWxp739kBuyR+CRhwu5nR" User-Agent: Xnews/2006.08.24 Mime-proxy/2.1.c.0 (Win32) Cancel-Lock: sha1:Qc5myUpj+5zdMW+RK1QBJmfWCzk= Xref: csiph.com comp.databases.ms-sqlserver:1145 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