48

I have a SQL Server 2012 database that I am trying to export to .CSV. My knowledge of SQL is very basic and my question is, I'm assuming it would be done in Management Studio, how do I do this?

Nenad Zivkovic
  • 18,221
  • 6
  • 42
  • 55
Webbly Brown
  • 1,010
  • 4
  • 15
  • 28

2 Answers2

59

If it is the query results you want exported to csv, you can right click on the results grid and select "Save Results As", which will allow you to save as csv.

Nick Howard
  • 943
  • 1
  • 13
  • 25
  • how would you export dates? – tichra May 11 '16 at 11:07
  • This does not save the headers – Uis234 May 14 '16 at 12:37
  • 1
    No, but you can do a "Select All" on the grid, then right click, and select "Copy with Headers" (or press Ctrl+Shift+C). You can then paste into excel, or wherever you like. – Nick Howard May 16 '16 at 06:02
  • 3
    In Sql Server Management Studio 2012 "Save Results As" included the headers for me. Just what I was looking for. – Jorge Cabot Oct 27 '16 at 16:22
  • 4
    To get the headers, go to the Query menu; select Query Options; navigate to Results > Grid and select "Include column headers when copying or saving the results" – thinkOfaNumber Apr 17 '18 at 02:58
  • 1
    Those previous instructions are **per-query.** To set this as the **default**, go to the Tools menu; select Options; navigate to Query Results > SQL Server > Results to Grid and select "Include column headers when copying or saving the results". – thinkOfaNumber Apr 17 '18 at 03:04
42

Right click on your database in management studio and choose Tasks -> Export Data...

Follow a wizard, and in destination part choose 'Flat File Destination'. Type your file name and choose your options.

Nenad Zivkovic
  • 18,221
  • 6
  • 42
  • 55
  • Thanks, I cant get the wizard to complete though. It gets through the first step, I select flat file and choose .csv format, then it returns this error: "Columns are not defined for this connection manager." – Webbly Brown Jul 04 '13 at 14:52
  • 1
    @WebblyBrown Are you sure you have selected source and destination correctly? This sounds like an error that you are choosing flat file as source rather then destination. First step in wizard is source, don't touch it, it should be DB where you clicked On 2nd step choose flat file as destination. – Nenad Zivkovic Jul 04 '13 at 14:59
  • 6
    Ok one last question I hope, how do I get it to select all the tables? The wizard takes me to a place where is says copy from one or more tables, however the next screen I cant work out how to select multiple or select all tables, right now its just exports tables one by one to csv... – Webbly Brown Jul 05 '13 at 08:29
  • 4
    If anyone else is having trouble, take a look at [this](http://blog.winhost.com/exporting-sql-server-data-with-import-and-export-wizard/) for full tutorial – benscabbia Nov 03 '15 at 14:30
  • This approach does not work if you are connecting to an Azure SQL Database. In that case, the "Save Results As" solution from Nick Howard is better. – bojingo Mar 09 '16 at 17:34