Saving results with headers in SQL Server Management Studio

SqlSql Server-2008ExcelSsmsExport to-Excel

Sql Problem Overview


I am using SQL Server Management Studio.

I wish to save the results of a query to an excel file.

I choose "save as" and then save to CSV file which I can open in excel. All good except I am missing column headers, any ideas how I get them exported?

Sql Solutions


Solution 1 - Sql

Tools > Options > Query Results > SQL Server > Results to Text (or Grid if you want) > Include columns headers in the result set

You might need to close and reopen SSMS after changing this option.

On the SQL Editor Toolbar you can select save to file without having to restart SSMS

Solution 2 - Sql

Try the Export Wizard. In this example I select a whole table, but you can just as easily specify a query:

enter image description here

enter image description here

enter image description here

enter image description here

(you can also specify a query here)

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Solution 3 - Sql

Another possibility is to use the clipboard to copy and paste the results directly into Excel. Just be careful with General type Excel columns, as they can sometimes have unpredictable results, depending on your data. CTL-A anywhere in the result grid, and then right-click:

enter image description here

If you have trouble with Excel's General format doing undesired conversions, select the blank columns in Excel before you paste and change the format to "text".

Solution 4 - Sql

At least in SQL Server 2012, you can right click in the query window and select Query Options. From there you can select Include Headers for grid and/or text and have the Save As work the way you want it without restarting SSMS.

You'll still need to change it in Tools->Options in the menu bar to have new query windows use those settings by default.

Solution 5 - Sql

The settings which has been advised to change in @Diego's accepted answer might be good if you want to set this option permanently. Permanently means it'll apply to all future query sessions that you open within SQL Server Management Studio(SSMS). This is usually not the case. Also, changing this setting requires restarting SQL Server Management Studio (SSMS). This is again a not-so-nice experience in case there are many unsaved query session windows open and you are in the middle of debugging something.

SQL Server gives a much slicker option of changing this setting on per session basis which is very quick and convenient. I'm detailing the steps below using query options window:

  1. Right click in query editor window. Now, click Query Options... in the context menu as shown below:

    enter image description here

  2. Select Grid under Results node in the left navigation pane. Now, check the Include column headers when copying or saving the results check box as shown below:

    enter image description here

That's it. Now, your current query session will start honouring your settings with immediate effect. You don't need restarting SSMS. Also, this setting won't be propagated to any future session either. Effectively, changing this setting on a per session basis is much less noisy.

Solution 6 - Sql

The same problem exists in Visual Studio, here's how to fix it there:

Go to:

Tools > Options > SQL Server Tools > Transact-SQL Editor > Query Results > Results To Grid

Now click the check box to true: "Include column headers when copying or saving the results"

Solution 7 - Sql

Select your results by clicking in the top left corner, right click and select "Copy with Headers". Paste in excel. Done!

Solution 8 - Sql

Got here when looking for a way to make SSMS properly escape CSV separators when exporting results.

Guess what? - this is actually an option, and it is unchecked by default. So by default, you get broken CSV files (and may not even realize it, esp. if your export is large and your data doesn't have commas normally) - and you have to go in and click a checkbox so that your CSVs export correctly!

To me, this seems like a monumentally stupid design choice and an apt metaphor for Microsoft's approach to software in general ("broken by default, requires meaningless ritualistic actions to make trivial functionality work").

But I will gladly donate $100 to a charity of respondent's choice if someone can give me one valid real-life reason for this option to exist (i.e., an actual scenario where it was useful).

Solution 9 - Sql

In SQL Server 2014 Management Studio the setting is at:

Tools > Options > Query Results > SQL Server > Results to Text > Include column headers in the result set.

Solution 10 - Sql

I also face the same issue. When I used right click in the query window and select Query Options. But header rows does not show up in output CSV file.

Then I logoff the server, login again and run the script. Then it worked.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondublintechView Question on Stackoverflow
Solution 1 - SqlDiegoView Answer on Stackoverflow
Solution 2 - SqlJohn DeweyView Answer on Stackoverflow
Solution 3 - SqlJohn DeweyView Answer on Stackoverflow
Solution 4 - SqlMichael McCormickView Answer on Stackoverflow
Solution 5 - SqlRBTView Answer on Stackoverflow
Solution 6 - SqlNicholas PetersenView Answer on Stackoverflow
Solution 7 - SqlChris DimitriuView Answer on Stackoverflow
Solution 8 - SqlDaibhi O DomhnaillView Answer on Stackoverflow
Solution 9 - SqlJason LewisView Answer on Stackoverflow
Solution 10 - Sqlhardik rawalView Answer on Stackoverflow