|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 9:16 AM
Points: 768,
Visits: 1,161
|
|
In SSMS 2008 query results window, you can right-click and choose "Copy with headers" (Copy, Copy with Headers, Select All)
if you didn't set the Option settings many mentioned above
Copy with Headers
FirstName MiddleName LastName 285 E Abbas 293 R. Abel
SQLServerNewbie
MCITP: Database Administrator SQL Server 2005
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, November 04, 2009 4:33 PM
Points: 1,
Visits: 13
|
|
David,
Actually there is a way to get the column names from the grid view in Sql Server Management Studio.
Goto:
Tools|Options| +Query Results +SQL Server Results to Grid Check the checkbox (Include column headers when copying or saving the results)
Peter
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 1:35 AM
Points: 4,789,
Visits: 1,336
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, November 02, 2012 8:38 AM
Points: 23,
Visits: 115
|
|
Thanks, David J. -- that'll do it! :)
As icing on the cake, starting from what you gave, I may prefer to let an export task do most of the work of creating the spreadsheet, and just use ADO in VBScript for:
objExcel.Range("A1:AD1").Font.Bold = TRUE objExcel.Range("A1:AD1").Interior.ColorIndex = 6
If you have comments, they are appreciated.
Thanks again...
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Sunday, September 05, 2010 6:28 AM
Points: 772,
Visits: 807
|
|
corneld (11/5/2008)
I concur with:  SSMS > Tools > Options > Query Results > SQL Server > Results to Grid > 'Include column headers when copying or saving results' Otherwise, good effort and nice article!!!
Indeed - you'd think this would be turned on by default, along with word wrap and display line numbers.
Good article - I've seen a similar Excel macro solution for Oracle, so I can see this has its applications.
www.abbstract.info - my blog www.sqlsimon.com - my experiences with SQL Server 2008
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Friday, March 08, 2013 11:15 AM
Points: 440,
Visits: 1,785
|
|
divison (11/5/2008) Thanks, David J. -- that'll do it! :)
...
Thanks again...
No problem :D
Here is another handy macro that demonstrates formatting, and tells you the numbers Excel uses for colours as a bonus
Sub colors56() '57 colors, 0 to 56 Application.ScreenUpdating = False Application.Calculation = xlCalculationManual 'pre XL97 xlManual Dim i As Long Dim str0 As String, str As String For i = 0 To 56 Cells(i + 1, 1).Interior.ColorIndex = i Cells(i + 1, 1).Value = "[Color " & i & "]" Cells(i + 1, 2).Font.ColorIndex = i Cells(i + 1, 2).Value = "[Color " & i & "]" str0 = Right("000000" & Hex(Cells(i + 1, 1).Interior.Color), 6) 'Excel shows nibbles in reverse order so make it as RGB str = Right(str0, 2) & Mid(str0, 3, 2) & Left(str0, 2) 'generating 2 columns in the HTML table Cells(i + 1, 3) = "#" & str & "" '& "#" & str & "" Cells(i + 1, 3).Font.Name = "Courier" Cells(i + 1, 4).Formula = "=Hex2dec(""" & Right(str0, 2) & """)" Cells(i + 1, 5).Formula = "=Hex2dec(""" & Mid(str0, 3, 2) & """)" Cells(i + 1, 6).Formula = "=Hex2dec(""" & Left(str0, 2) & """)" Cells(i + 1, 7) = "[Color " & i & "]" Next i done: Application.Calculation = xlCalculationAutomatic 'pre XL97 xlAutomatic Application.ScreenUpdating = True End Sub
This is a macro to be run in excel, try it in a new workbook. All credit to http://www.mvps.org/dmcritchie/excel/colors.htm ;)
Dave J
http://glossopian.co.uk/ "I don't know what I don't know."
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, December 15, 2010 9:28 AM
Points: 1,
Visits: 10
|
|
| When I need to grab column names to paste into Excel, I just push the "Results to Text" button, run the query, copy the column names and paste them into Excel, then switch back to "Results to Grid" and rerun the query. It works for anyone who only infrequently needs to copy column names.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: 2 days ago @ 1:00 PM
Points: 195,
Visits: 1,252
|
|
Thank you David J It seems we are thinking at the same direction. You can do it in VBA and VB script. In fact some time I development and tests in VBA (testing and editing macros) and then translate the result to VB script. But so far we have been talking about formating. If you need more complicated actions depending on actual data like subsums filtering linking to other sheets etc I stay with ADO and VBA because it works. And it is really fashinating what you can do with Excel. I have for a client made a "customer simulator". This workbook is loaded with 6 different queries (in different sheets) from the data ware house and there is a lot of logic inside. A salesman can key in a prospect and simulate different situations to calculate the assumed net margin.
:) Gosta
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: 2 days ago @ 9:57 AM
Points: 14,
Visits: 114
|
|
| Like others before me, I've always used export tools to get the data to Excel. However, the formatting of the spreadsheet for alternate colors makes me *almost* wish that I knew more about VB. Execllent on formatting! ~Clarie
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, May 09, 2011 12:46 PM
Points: 1,
Visits: 0
|
|
|
|
|