Solved

Export to CSV with Column names

Posted on 2014-10-30
3
188 Views
Last Modified: 2014-10-30
Hi.
I have the following. How can I export to CSV and include the column names in the first row of the CSV file?
Any ideas

The following works but does not include the column names.

ALTER  procedure [dbo].[EXPORT_TO_CSV]
(
 @SQL NVARCHAR(4000), @FilePath nVARCHAR(200)
)
as
BEGIN
declare @s varchar(5000)
set @s = 'bcp "'+ @SQL + '" queryout ' + @FilePath + ' -c -t, -T'
exec master..xp_cmdshell @s
print @s
END
0
Comment
Question by:JElster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40413854
I suggest you use a UNION ALL,

such as

bcp "select 'Column1', 'Column2', 'Column3' "'+ @SQL + '" queryout ' + @FilePath + ' -c -t, -T'
0
 
LVL 1

Author Comment

by:JElster
ID: 40413895
So your saying I need to manually add the field names and then do a union all?
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40413896
Yes.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question