Avatar of Varshini S
Varshini S
 asked on

SQL QUERY TO CSV WITH COLUMN HEADER

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
Microsoft SQL Server 2008Microsoft SQL Server 2005

Avatar of undefined
Last Comment
Aaron Shilo

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Surendra Nath

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Aaron Shilo

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

Your help has saved me hundreds of hours of internet surfing.
fblack61