Showing posts with label tsql. Show all posts
Showing posts with label tsql. Show all posts

SSMS Copy Column Headers

Needing some sort of variation or at least a break from my certification studies I decided to revisit a title I purchased a while back: "Beginning SQL Server 2008 for Developers" from @Apress written by Robin Dewson. Truly glad I did because one of the problems I have when illustrating data related issues to clients is having to piece together a bloody spreadsheet. I typically would query the tables then query the INFORMATION_SCHEMA.COLUMNS or sys.columns to get the column names to use as headers. Then I would copy and paste them into the predetermined rows.

Well not any more because page 42-43 of this awesome book had life changing screenshots of the Options dialog. Big deal right? Well a big deal for me at least! Following the path of Query Results --> SQL Server --> Results to Grid lead me to a sudden state of euphoria. There I saw it as the lights of heaven filled my home, blurring everything around me as if a spotlight and magnify focused in on the wonderful words that read: "Include column headers when copying or saving the results". Was it really true, are my days of querying sys.columns or the INFORMATION_SCHEMA.COLUMNS finally over? In a way, maybe, not really, but at the very least delivering requested spreadsheets to clients will be a bit faster. I was so excited I tried this on MSSQL 2005 and sure enough it is there!

What you need to do is: 

Step 1 - Go to Tools --> Options

Step 2 - Expand Query Results --> SQL Server --> Results to Grid

Then check the box titled: Include column headers when copying or saving the results

Step 3 - Copy some cells

If you have tabs opened already, open a new query window and try it. You should see the menu item "Copy with Headers" as a right click option

Step 4 - Then paste your results into the spreadsheet

Just a side note, if you are dealing with datetime data types you should consider making the predetermined columns in the spreadsheet as a text category by formatting the cells, under the Number tab select the "Text" category.

Cover image taken from Apress.

Posted via email from wetmatter nonsense

sqlshots: What version are you running?

1. This option is okay but not what I needed.

SELECT @@VERSION AS [Version Info]

2. This option is okay as well but does not indicate if it is 2000, 2005, 2008. Yeah it shows the product version number, but if you don't readily know it then you have to look it up.

SELECT SERVERPROPERTY('productversion')  AS [Version]         ,SERVERPROPERTY('edition')            AS [Edition]         ,SERVERPROPERTY('productlevel')     AS [Service Pack]

3. This option works okay but the you'll have issues with the Edition as the char length will differ

SELECT RIGHT(LEFT(@@VERSION,25),15)  AS [Product]        ,RIGHT(LEFT(@@VERSION,40),12) AS [Product Version]         ,LEFT(RIGHT(@@VERSION,65),17) AS [Edition]          ,SERVERPROPERTY('productlevel')  AS [Service Pack]

4. This is the winner for now (not great for version 6.5 & 7 as some additional char proceeds after due to the char length) 

SELECT RIGHT(LEFT(@@VERSION,25),15)    AS [Product]       ,RIGHT(LEFT(@@VERSION,40),12)   AS [Product Version]       ,SERVERPROPERTY('edition')      AS [Edition]       ,SERVERPROPERTY('productlevel') AS [Service Pack]

5. After some thought I like this route better

SELECT RIGHT(LEFT(@@VERSION,25),15)    AS [Product]    ,SERVERPROPERTY('productversion')   AS [Version]    ,SERVERPROPERTY('edition')          AS [Edition]    ,SERVERPROPERTY('productlevel')     AS [Service Pack]

I haven't tried this with any other version other than 2005, but I suspect 2000 & 2008 will work just fine. Earlier versions like 6.5 & 7 I am not sure of.

Posted via web from wetmatter nonsense