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.
Jake Churchill says:
Nice post. I usually do the results to text and save as a csv file but then I always have to open that with excel and make sure to re-save as an excel file. This might actually save a step.
2 January 2013, 12:00 pmRyan says:
Yea I’ve done it that way too, but I’ve run into problems when there are commas in the data. I don’t think SQL Studio escapes them.
2 January 2013, 12:03 pmChris Tierney says:
Great tip! Thanks Ryan.
8 January 2013, 12:13 pm