Column Headings from SQL Results

I have had some one-off requests to basically dump data from SQL Server to an Excel spreadsheet.  I am the kind that wants to make the spreadsheet look decent, so I would like to at least have column headings.  The below process provides a less painful way to get column headings from a SQL results grid into Excel. 

In SSMS, go to Query>Results To>Results to Text

1

Run the query
Copy the column heading line and paste into NotePad

2

Next, put the headings on separate lines by doing CTRL-Right Arrow-Enter until you hit the end of the list

3 

Copy the list and paste into Excel starting on line 2
Once in Excel, do a Ctrl C, select A1 and Paste/Paste Special and Check transpose (ok)

4

As you can see the column headings are now on Row A

5

Go back to SSMS and go to Query>Results To>Results to Grid
Run the query
6

Copy the contents and paste it in the Excel file starting in Row B

7   

This process helps tremendously when the result set contains more than 20 fields; however, if you find yourself pulling the same data on a regular basis, I recommend using SQL Server Integration Services or Excel automation.

Posted on April 23, 2008, in SQL Server. Bookmark the permalink. 6 Comments.

  1. scottstonehouse

    Try this:


    In SSMS, under tools–>options

    In the tree on the left, go to Query Results–>SQL Server–>Results to Grid

    In the list on the right, check “Include column headers when copying or saving the results”.

    Click OK.

    Now the headers should be copied automatically for you. Why this isn’t the default setting is a mystery to me.

  2. Wow. Thanks Scott. Now this is what the Internet and Blogs are all about. To learn… I had no idea about this setting and am very anxious to pass it on.

  1. Pingback: Column Headings from SQL Results - Easier Way « Rhonda Tipton’s WebLog

  2. Pingback: Dew Drop - April 24, 2008 | Alvin Ashcraft's Morning Dew

  3. Pingback: Social Networks/Blogging and Self-Improvement « Rhonda Tipton’s WebLog

  4. Pingback: jQuery 1.4 Reference Guide | Wow! eBook – Great ebook, great site! | AJAX WebDev Insider

Leave a Reply

Please log in using one of these methods to post your comment:

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.