Export Query Results as CSV WITH Headers

Dom 771 Reputation points
2023-03-28T21:32:41.3666667+00:00

I just need to get my query results out to a CSV file WITH Headers quickly (without going through the whole SSIS process for a one-off job). I thought the option below would do the trick, but I get a CSV WITHOUT Headers. Advice?

User's image

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,163 questions
{count} votes

Accepted answer
  1. LiHongMSFT-4306 25,326 Reputation points
    2023-03-29T02:43:09.4266667+00:00

    Hi @Dom

    Tools -> Options -> Query Results -> SQL Server -> Results to Grid -> Include columns headers when copying or saving the result.

    You might have to close and restart SSMS after changing this option.

    Then right click the grid result and select Save Result As...

    User's image

    And got this csv file:

    User's image

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    5 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Michael Taylor 50,506 Reputation points
    2023-03-28T21:38:26.3633333+00:00

    In SSMS? Run your query and get the results in the grid. Right click anywhere in the grid and select Save Results As. Enter the filename and click save. It exports to CSV with headers.

    1 person found this answer helpful.

  2. Erland Sommarskog 104.7K Reputation points MVP
    2023-03-28T21:53:59.6+00:00

    I tried the option you have in the picture, and I do get headers:

    User's image

    Note that you need to open a new query window for the setting to take effect.

    And keep in mind that this is a fairly simple-minded option. You may note that in the above there are three commas per line, despite that there are only three header columns. This is because the values in the Amount column are being formatted to to my regional settings. But SSMS is not doing anything to save the situation. The same would be if there is comma in a string.