Solved

SQL QUERY TO CSV WITH COLUMN HEADER

Posted on 2013-12-29
2
6,561 Views
Last Modified: 2013-12-30
I am using following BCP command to export the SQL out put to CVS file. But it is missing the column header. How do I add the column header in the CSV out put ?      

      select @sql = 'bcp "exec '+  'usp_EmployeeDetails]'''+ @myDate+ '''" queryout ' + @Path + '.csv -c -t, -T -S'+ @@sname

      EXEC master..xp_cmdshell @sql
0
Comment
Question by:Varshini S
2 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39744858
There is no option in bcp which will export the headers along with the data
the only way to do to this is to manually add the headers...

I believe your USP_employeeDetails will have some select statement at the very end
some thing like

SELECT Emp_id,Emp_name,.... from Employess ... something similar to this.

so what I would do is something like this

select 'Employee ID', 'employee name',....
union all
SELECT Emp_id,Emp_name,.... from Employess

which will also give you the headers...

in short no change to BCP, but only to USP...
0
 
LVL 15

Expert Comment

by:Aaron Shilo
ID: 39746830
hi

the way to do this is use a query like so:
  bcp "select 'col1', 'col2',.. from dbo.myTableout" queryout myTable.csv /SmyServer01 /c /t, -T

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

943 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now