Saving Results with Headers in SQL Server Management Studio

Saving results with headers in SQL Server Management Studio

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

SQL Management Studio exports CSV with header from one server and without header from other server

@destination-data You got me on the right track. I restarted Management Studio and since then I get the save as results as specified in the options. Kinda embarrassing that I didn't think of restarting. Worked fine for days.

Added to mark this as answer.

Exporting from SQL Server to Excel with column headers?

I typically do this by simply click the upper left corner in the results grid, copy, and then paste into Excel. There is one catch, you need to go into options->query results-> SQL Server->results to grid (or text if you want to save to file for import into excel) and turn on include column headers when copying or saving the results. I find this works great.

SSMS :: Copy with Headers and paste to Excel results in more rows in destination

I guess your varchar(max) causes the problem. The size of a text field in Excel is limited. Following an example:

DECLARE @x VARCHAR(MAX) = '***************************************************************************';
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)

SELECT @x AS Test, 'Test' AS Test2

Copy the result to an excel file and you will get two rows, where the second column (Test2) is only featured in the second row.

Export query result to .csv file in SQL Server 2008

  1. Open SQL Server Management Studio
  2. Go to Tools > Options > Query
    Results > SQL Server > Results To Text
  3. On the far right, there is a drop
    down box called Output Format
  4. Choose Comma Delimited and click OK

Here's a full screen version of that image, below

Sample Image

This will show your query results as comma-delimited text.

To save the results of a query to a file: Ctrl + Shift + F

How can we change the default settings for Save Query result to File in SSMS

SSMS will export the same things defined (and displayed) as if you would run a Result to Text. In fact, you can define everything under the menu point Result to Text and it will work for both.

If your missing something here, you can also use BCP (more infos here) or PowerShell to export data from an table, view or procedure.



Related Topics



Leave a reply



Submit