title: export sql table data with column names to csv file
body: <p>I am trying to export the data from sql server tables to a csv file without ssms.</p>
  
  <p>i am trying to achieve it by creating a stored procedure using bcp.</p>
  
  <p><code>declare @sql nvarchar(4000);
  select @sql = 'bcp "select * from table" queryout c:\file.csv -c -t, -T -S'+ @@servername
  exec xp_cmdshell @sql</code></p>
  
  <p>1 ) This query produces the expected results. But what i want is it should also include the column names in the csv files. So how can i achieve that ?</p>
  
  <p>2) I want this result for all the tables in the given database. So how to do that ?</p>
  
  <p>Please give some suggestions or solution as soon as possible</p>
  
  <p>thanks</p>