?
Solved

SQL QUERY TO CSV WITH COLUMN HEADER

Posted on 2013-12-29
2
Medium Priority
?
7,981 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

864 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