join the MSSQLTips community

Today's Site Sponsor


 

SQL Compare quickly and easily compares and synchronizes SQL Server database schemas
 



What's slowing you down?

Copying column headers with grid query results in SQL Server 2005 Management Studio

Written By: Greg Robidoux -- 11/7/2006 -- read/post comments -- print -- Bookmark and Share

Rating: (not rated yet) Rate

Problem
One of the nice features with SQL Server is the ability to create result sets from queries into a grid result set. This data can then be copied and pasted in other application such as Excel.  The downside to saving the results in a grid is that the column headers don't get copied along with the data.  To get around this you could query the data in the text format, so you could copy the results along with the column headers, but then you are faced with formatting issues.

Solution
One change with SQL Server 2005 is a new option to copy column headers along with the data results.  This option gets set using the query options setting.  To access this setting from SQL Server Management Studio, select Query | Query Options from the menus and you will see the following screen:. 

Select the Results / Grid setting and check "Include column headers when copying or saving the results".  Once this is set whatever query you run and then if you select the results and copy and paste into another application the column headers are also copied along with the data.

Here is a sample query:

select top 5 name, id, crdate from sysobjects

Results copied with column headers off:

sysrowsetcolumns 4 10/14/05 1:36
sysrowsets 5 10/14/05 1:36
sysallocunits 7 10/14/05 1:36
sysfiles1 8 4/8/03 9:13
syshobtcolumns 13 10/14/05 1:36

Results copied with the column headers on:

name id crdate
sysrowsetcolumns 4 10/14/05 1:36
sysrowsets 5 10/14/05 1:36
sysallocunits 7 10/14/05 1:36
sysfiles1 8 4/8/03 9:13
syshobtcolumns 13 10/14/05 1:36

Next Steps

  • This is a simple little tip, but could save you time and headaches trying to get the column headers copied
  • SQL Server Management Studio can be used to manage your SQL Server 2000 databases, so if you have a need to copy the column headers along with the data look at using SQL Server Management Studio.
Readers Who Read This Tip Also Read Free Live Webcast Comment or Ask Questions About This Tip


Sponsor Information
Free SQL Server performance monitoring dashboard – Idera SQL check

Try Red Gate SQL Multi Script: Execute multiple scripts against multiple SQL Servers with one click!

You don't know, what you don't know about SQL Server... Customized Consulting and Training

Stop here to prepare for your next SQL Server interview!

Free whitepaper - Top 10 Things You Should Know About Optimizing SQL Server Performance


Get Our Tips Newsletter

We keep 50,000+ SQL Server professionals informed.



Idera - SQL defrag manager

SQL defrag manager is a one-of-a-kind solution that automates the time-consuming process of finding and fixing database index fragmentation issues across multiple SQL Servers. SQL defrag manager improves server performance by analyzing database index fragmentation levels, pinpointing fragmentation “hot spots” and taking action to defragment automatically, or at your command.

Download now!

More SQL Server Tools
SQL comparison toolset

SQL secure

SQL Prompt

SQL safe backup

SQL Compare




Copyright (c) 2006-2010 Edgewood Solutions, LLC All rights reserved
privacy statement | disclaimer | copyright | advertise | write for mssqltips | feedback | about
Some names and products listed are the registered trademarks of their respective owners.


CareerQandA.com | MSSharePointTips.com | MSSQLTips.com