SQL Server Admin
T-SQL Articles

January 24, 2010

Using SQLCMD to export SQL Server Data as Comma Seperated Values




I have doing a couple of posts on SQLCMD recently (here and here) and have received some interesting questions too. One of the questions that caught my attention was to export SQL Server Data as CSV’s using SQLCMD.

Here’s the query to so do:

sqlcmd -S Suprotim-PC -d Northwind -E -Q "SELECT CustomerID, CompanyName, ContactName from Customers" -o "D:\MyData.csv" -s","

This query extracts data from 3 columns from the Customers table of the Northwind database and saves it in D:\MyData.csv

After running the command, here’s the output received

image


Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter



 
  Feedback:

comments

2 Responses to "Using SQLCMD to export SQL Server Data as Comma Seperated Values"
  1. Pollus said...
    January 25, 2010 10:23 AM

    There's an easier way to export a CSV with BCP. Or you could use a script like this one ! http://www.pollusbrodeur.com/wiki/ow.asp?CSV.BAT

  2. Suprotim Agarwal said...
    January 26, 2010 12:03 AM

    Thanks Pollus. I will check it out!

 

Copyright © 2009-2011 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions