I often need to write ad-hoc reports in MS SQL Studio, and then provide those results in an Excel spreadsheet. This has usually been a little tedious, I copy & paste the results from SQL Studio into Excel, then have to manually add column names to the Excel spreadsheet. So I finally spent some time figuring out how to do this better, and discovered that it is possible to have the column names come over automatically. I’m sure many of you already knew about this, but I didn’t so I thought it might be worth sharing. I had tried googling this in the past and didn’t come up with anything, so maybe it’s not widely known.
It’s as simple as checking a box in the settings of MS SQL Studio. These instructions are for SQL Studio 2005, it may be different in later versions. Go Tools->Options and then Query Results->Results To Grid. Then check the box for Include column headers when copying or saving the results.
There are a few ways to get these results into Excel, but this is what I do. After running the query, I click on the blank box that intersects the row numbers and the column headers. This causes all the rows & columns to be selected. You could also press Ctrl+A to do the same thing.
Then I copy the results using Ctrl+C, then switch over to my Excel window and paste the results in. And voilà, the column headers are there.