How to save query results without headers as a file from SQL Server Management Studio.

knt1N 286 Reputation points
2021-07-01T04:28:08.037+00:00

In Object Explorer, right-click the target table to select data for, and then click "Select Top 1000 Rows".
Then right-click on the top left of The "Results", and then click "Save Results As...".
The question of the case of saving the file in this way.

Saved files contain headers. I want to save without headers.

Strangely enough, if i copy the auto-generated SQL at this time and then click "New Query" and run the exact same SQL pasted into a new tab, there is no header if i save the file in the same operation.

The options are set as follows:

  1. Tools > Options > Query Results > SQL Server > Results To Grid
  2. "Include column headers when copying or saving the results" is OFF.

If this setting is turned ON, it will always be saved files contain header, even in the case of query results selected by "New Query".
The way I want to know is to save the query results without headers in any case.

Currently, I am correcting the saved file myself, but it is laborious because there are many opportunities to do it.
Also, if you can't, I want to explain the cause to my stakeholders.

I'd appreciate it if someone could answer my questions.

==================================================

The state of my environment about the settings I was taught in reply from Viorel-1.
About "Include column headers when copying or living the results" ON/OFF.

1) Tools > Options > Query Results > SQL Server > Results To Grid
Always OFF.

2) Query > Query Options... > Results > Grid
When executed from "New Query", it is OFF.
When executed from "Select Top 1000 Rows", it is ON.
I always want this to be OFF, but no matter how many times I set it to OFF, it is always ON when I run "Select Top 1000 Rows".

I'd appreciate it if someone could answer my questions.

SQL Server Other
{count} votes

Accepted answer
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-07-02T08:53:38.727+00:00

    Settings in SSMS can be a bit of mystery when they take effect and how they stick.

    For instance, I have checked that I want results in a separate tab, but when I use SELECT TOP 1000 Rows (which I normally never use), I still get the divider that is the default.

    However, when I did as Cris said, and changed the setting under Results to Text (i.e. not Results to Grid!), and then opened SELECT TOP 1000 Rows, and pressed Ctrl-Shift-F to direct output to file, the data was saved without headers.


1 additional answer

Sort by: Most helpful
  1. Cris Zhan-MSFT 6,661 Reputation points
    2021-07-01T06:05:19.767+00:00

    Hi,

    In my test, if I do not check "Include column headers in the result set" under Tools> Options> Query Results> SQL Server> Results To Text, and then click "Select Top 1000 Rows" to open a new query windows, there will be no column names when output the query results to a file.

    The key is that after changing the options/settings, if there is currently an open session ("Select Top 1000 Rows"), you need to close and reopen the new session window. To apply the new option.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.