Export Query Results to CSV by using SQLCMD

This is indeed very easy process and very simple command to export any query data. For example we will use AdventureWorks2012 database. Here is the query we will be using for our demonstration.
USE AdventureWorks2012
GO
SELECT TOP 10 sp.BusinessEntityID,sp.TerritoryID, sp.SalesQuota,sp.Bonus, sp.CommissionPctFROM Sales.SalesPerson sp
GO
The above query will return following result set.
Now we can export above data to CSV using SQLCMD using following command.
SQLCMD -S . -d AdventureWorks2012 -Q “SELECT TOP 10 sp.BusinessEntityID, sp.TerritoryID, sp.SalesQuota, sp.Bonus, sp.CommissionPct FROM Sales.SalesPerson sp” -s “,” -o “e:\result.csv”
Generically you can use the following syntax:
SQLCMD -S YourSQLServer -d YourDatabase -U YourUserName -P YourPassword -Q “Your Query” -s “,” -o “C:\Yourfilename.csv”
Now you can go to your file location and open the file and you will see that new csv file created there. When you open the csv file you will notice the results of the query.

Comments

  1. This is much like Twitter in that you get to tell others what you are doing.
    Buying fans or views is a clean process and delivered to you on time.
    It has to be checked and ensured that the fans and followers on Facebook and Twitter respectively are active.


    Feel free to visit my blog; http://socialbears.com/youtube-views/

    ReplyDelete
  2. Can I simply say what a relief to discover somebody who
    genuinely understands what they are talking about online.
    You actually realize how to bring a problem to light and make it important.

    A lot more people must look at this and understand this side of your story.
    It's surprising you aren't more popular given that you certainly possess the gift.


    My web page :: girls
    my web site :: girlsbcn

    ReplyDelete
  3. There are many sources to choose from that can offer simple solutions to the problems you are experiencing with
    your canine. Citrus juices help in decreasing the chance
    of bacteria to remain in your dog. Research states that
    more dog trainers and owners are deciding on the dog training collar.


    Feel free to visit my site - dog training advice online

    ReplyDelete
  4. Hi there, I found your web site via Google
    while searching for a related subject, your site came up, it seems
    great. I have bookmarked it in my google bookmarks.

    Hello there, simply turned into alert to your weblog through Google, and found that
    it's really informative. I'm going to watch out for brussels.
    I'll appreciate when you continue this in future. Lots of folks will probably be benefited out of your writing. Cheers!

    my web blog :: Accounts demystified

    ReplyDelete
  5. Somebody necessarily assist to make seriously posts I'd state. This is the very first time I frequented your website page and to this point? I surprised with the analysis you made to make this particular submit incredible. Magnificent process!

    my blog: 1st birthday party ideas

    ReplyDelete

Post a Comment