Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Get column names in first row of result set


Get column names in first row of result set

Author
Message
Ken Davis
Ken Davis
SSC-Enthusiastic
SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)

Group: General Forum Members
Points: 171 Visits: 491
Thanks for the ideas Sean and Lynn. I'm actually helping another DBA. There are a few other requirments such as stripping out some characters and he wrote a query to accomplish that. So I was trying to make it work with his query.

But *duh* I can drop his query in the export wizard and check that box to include column headers. Going to try that now...
Evil Kraig F
Evil Kraig F
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5695 Visits: 7660
I would agree with SSIS in general, but if that's not an option and you must stay in T-SQL the easiest way is to quick-hack the results for BCP, like so (see the second post down):

http://stackoverflow.com/questions/1355876/export-table-to-file-with-column-headers-column-names-using-the-bcp-utility-an

It's similar to the above discussed union but it's forced into BCP without having to fight with sorting components.


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
SanKuKai
SanKuKai
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 4
Hi,

I had the same issue from the same type of client.
I sorted it out with a bit of dynamic SQL to create the Union query.

Note: because all the data must be converted to varchar due to the new first line containing column titles, formats need to be taken into account (for dates at least).


Declare @TableName nvarchar(max)
Declare @ListColumns nvarchar(max)
Declare @ListColumnsConverted nvarchar(max)
Declare @QueryColumnNames nvarchar(max)
Declare @QueryTableValues nvarchar(max)
Declare @QueryHeaderAndRows nvarchar(max)

Set @TableName = 'YourTable'

--Get the column hearders from the system table
SELECT @ListColumns = coalesce(@ListColumns + ', ', '') + convert(varchar(500),COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
ORDER BY ORDINAL_POSITION

--Get the same list of column from the system table but adding a convert statement (All columns must be converted to varchar because of the first row containing only alphanumeric)
SELECT @ListColumnsConverted= STUFF((SELECT ',' + Case When DATA_TYPE = 'datetime' Then
                                       'Convert(VARCHAR(11), ' + convert(varchar(max), COLUMN_NAME) + ',103)'
                                     Else
                                       'Convert(varchar(max),' + convert(varchar(max), COLUMN_NAME) + ')'
                                     End
                            FROM INFORMATION_SCHEMA.COLUMNS
                            WHERE TABLE_NAME = @TableName
                            ORDER BY ORDINAL_POSITION
                            FOR XML PATH('')), 1, 1, '')

-- Create the Select statement containing only the headers names
SET @QueryColumnNames = 'Select ''' + Replace(@ListColumns, ', ', ''', ''') + ''''

-- Creating the statement with the values
SET @QueryTableValues = 'Select ' + @ListColumnsConverted + ' From ' + @TableName

-- Union all on both
SET @QueryHeaderAndRows = @QueryColumnNames + ' Union All ' + @QueryTableValues

EXEC (@QueryHeaderAndRows)


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search