#0209 – SQL Server-SSMS – Include column headers when copying query results


As part of my work, I often need to copy-paste the results of a query into an Excel sheet for multiple reasons. The purpose of such a requirement could be as simple as having a record of what the data looked like before and after a change I made (as in when doing data cleanup or custom data modification work) to as complex as running an ad-hoc data analysis. Almost all of my production-grade queries use aliases and it would be the same aliases that I would prefer to have as column headers on the Excel sheets for sake of consistency. The easiest way to achieve this is to copy the headers along-with the data from the results tab of the SSMS window.

Traditionally, I would choose the option “Copy with Headers” popup-menu option (or use the equivalent Ctrl+Alt+C keyboard shortcut):

image

However, our memories do fail us sometimes and remembering to use “Copy with Headers” and not the plain “Copy” is error-prone. Hence, I have the following setting turned on in my SSMS options:

image
Go to the Options window using Tools –> Options.
Under Options, navigate to Query Results section.
image
Go to the “Results to Grid” option and check the check-box labeled “Include column headers when copying or saving the results”.

Repeat the process, if required for “Results to Text” output format.

Re-launching the SSMS and attempting to copy (just plain copy) the result set also copies over the column headers to the applications of my choice, including Excel.

So, here are my questions – Do you use this built-in feature of the SSMS? Do you have any others to share with the group?

Until we meet next time,

Be courteous. Drive responsibly.

Advertisements

4 thoughts on “#0209 – SQL Server-SSMS – Include column headers when copying query results

  1. dishdy

    Ah yes! You have finally showed me how to keep this set permanently. I always did it via Query/Query Options/Results/Grid but every time I exited and entered SSMS this setting would go away.

    Like

    Reply
  2. Nakul Vachhrajani

    @dishdy: It’s great to know that this post helped in making your day-to-day work simpler. Keep the feedback coming, and thank-you very much for reading!

    Like

    Reply

Let me know what you think about this post by leaving your feedback here!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s