Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving results with headers in Sql Server Management Studio

People also ask

How do I export query results to excel in SQL Server Management Studio?

In Object Explorer, right-click the required database/table/view and click Export Data on the shortcut menu to invoke the export wizard. On its first page, Export format, select the preferred format: MS Excel (. xls) or MS Excel 2007 (. xlsx).

How do I save a SQL query in SQL Server Management Studio?

To save a query: Use the Save command in the application toolbar. In the Save File As window, choose a name and a location to save your query.


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


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


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".


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.


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"


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