?
Solved

SQL QUERY TO CSV WITH COLUMN HEADER

Posted on 2013-12-29
2
Medium Priority
?
7,493 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
[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 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

762 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