|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, April 01, 2009 1:30 PM
Points: 7,
Visits: 33
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, October 07, 2009 1:56 PM
Points: 2,
Visits: 14
|
|
SSMS > Tools > Options > Query Results > SQL Server > Results to Grid > 'Include column headers when copying or saving results'
... the macros good though :)
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Saturday, October 31, 2009 3:35 AM
Points: 143,
Visits: 756
|
|
"Excel is a great tool for formatting data that you've extracted from tables or views. You can copy such data into an Excel spreadsheet straight from SQL Management Studio's output grid. Unfortunately there's no way to also grab the column names, so unless you have some way of generating them, you're stuck either omitting them or filling them in by hand."
David I like your articel but one question: As you incoporate VBA macros why not use ADO as alternative to get out the data from SQL Server. This is easier and you can get the column the names by a method like: Just part of the script I use this one to print TAB sep files with column headers Dim rs As New ADODB.Recordset Dim utskriftrad as string Dim K as long cmd.CommandText = sqltext 'An SQL statement rs.open cmd
For K = 0 To rs.Fields.Count - 1 utskriftrad = utskriftrad & rs.Fields(K).Name & Chr(9) Next K print utskriftrad etc
I yesterday wrote an article to SQL ServerCentral maybe that one will be accepted. And there is a lot more to write how to interact with Excel.
//Gosta
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Wednesday, November 04, 2009 8:19 AM
Points: 350,
Visits: 1,339
|
|
I realise I'm a bit off-topic, as what you are aiming to do here is a semi-manual process. but all the things you do here, and more, can be done via OLE automation. Robyn and I wrote something a while back which will work in SQL Server 2000 as well http://www.simple-talk.com/sql/t-sql-programming/sql-server-excel-workbench/
The advantage in using the metadata from ADODB or odbc is that it will work for any result to give you your column titles, not just a table or view.
Best wishes,
Phil Factor Simple Talk
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Saturday, October 31, 2009 3:35 AM
Points: 143,
Visits: 756
|
|
Phil Thank you for pointing at your article. It was new for me. But still why this labor in T-SQL when you can do all in VBA coding?
Best regards Gosta
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Wednesday, November 04, 2009 8:19 AM
Points: 350,
Visits: 1,339
|
|
You can do a lot of this by simply using VBA from Excel. I've done it both ways. If the structure and formatting of the spreadsheet requires knowledge of the data, or maybe if it is a timed report, or requested report, to be sent out via email using an Excel file attachment, then I do it from TSQL. Where it is a much simpler report than VBA is the obvious strategy. There are a lot of ways of producing Excel spreadsheets from database data, and the more techniques you have in your armory, the more likely you are to find one that is a good fit for a particular requirement.
Best wishes,
Phil Factor Simple Talk
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Wednesday, October 14, 2009 7:19 AM
Points: 404,
Visits: 1,130
|
|
Nice Macro. :)
The only thing I'd add to the Sub ColorAlternateDataRows is this line at the start
Application.ScreenUpdating = False
And at the end
Application.ScreenUpdating = True
Then it runs much quicker
HTH
Dave J
http://glossopian.co.uk/ "I don't know what I don't know."
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, October 15, 2009 12:32 PM
Points: 2,
Visits: 21
|
|
The technique from this article doesn't seem to retain the correct data types from the database.
For example a number (data type varchar in SQL) from the SQL Output grid with a leading zero, once copied into Excel with loose it's leading Zero as it will be converted to Number. Even if you format the whole spreadsheet to text before you copy the results, its will then mean real numbers are converted as text and you can't do any more sums in Excel.
This means this technique is not really usefull as a fully automated technique. You will have to spend some time setting the Excel column cell data types acordinly. Which is a requirement from most of us i guess.
However using the SQL Server data export functionality (right click on a table, "export") will retain column names and data types. And is really as automated as running the code from the article. I would say even more !
Excel formatting bit is usefull though and the stored procedure / use of schema an eye opener.
thanks
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, September 18, 2009 9:21 AM
Points: 6,
Visits: 7
|
|
The article states that unfortunately there's no way to also grab the column names from SQL Management Studio's output grid, but there is a checkbox option on the Query Options dialogue box that reads "Include column headers when copying or saving the results" that works great to bring along the column headers.
Additionally, Excel 2007 now has built-in table formatting that allows you to automatically apply different colors based on conditional values from any column.
Respectfully,
LGW
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Monday, September 28, 2009 3:30 AM
Points: 214,
Visits: 23
|
|
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!!!
|
|
|
|