Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Result of a SQL query into an EXCEL sheet

Author  Topic 

divan
Posting Yak Master

153 Posts

Posted - 2012-08-16 : 10:38:22
I have been writing many sql quires and when I run them then I do a cut and past into an EXCEL sheet and then I have to type in the column. Is there a way where we can direct the output of a SQL query into an EXCEL sheet where it will also print the column headers?

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-08-16 : 11:07:31
If you have the results in a grid then select all then right click and "copy with headers"
When you paste into excel it will give you the headers (also useful for copying to a where clause).

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-08-16 : 11:19:54
See here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

divan
Posting Yak Master

153 Posts

Posted - 2012-08-16 : 11:23:42
Well I must be missing something.. I do a select all and then I do a right click but I do not see "copy with headers"
Go to Top of Page

divan
Posting Yak Master

153 Posts

Posted - 2012-08-16 : 12:54:46
Folks I have tried all the above and none of them are working... Any other suggestions.. I am really desperate for a solution..

Thanks
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-08-16 : 13:34:46
Assuming you are on SQL 2008 or above, what Nigel said should work - i.e.: 1) click in the results pane and control-A to select all, b) right click and select copy with headers option.

If you don't see that menu, go to Tools->Options -> Query Results -> SQL Server -> Results To Grid and check the "include column header when copying or saving results" checkbox and try again. But I don't have that checked and I still get the right-click menu to include column headers, so not sure if that will help.
Go to Top of Page

divan
Posting Yak Master

153 Posts

Posted - 2012-08-16 : 14:29:13
UNFORTUNATELY I AM ON SQL 2005 SO DOES THAT MEAN THERE IS NO SOLUTION???
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-08-16 : 14:51:22
SQL 2005 does not have the right-click menu option for "include headers". But, if you go to Tools->Options->Query Results->SQL Server->Results To Grid, there is a checkbox for "Include column headers when copying or saving results".

It works on my SQL 2005 RTM, so it should work on yours as well.
Go to Top of Page
   

- Advertisement -