title: I need best practice in T-SQL Export data to CSV (with header)
body: <p><strong>What I need to do is export data into CSV file using T-SQL.</strong></p>
  
  <p>And I'm very confused about there are many ways can do it, I don't know to choose which one, please help me to confirm the bollowing:</p>
  
  <p>As I know there are about 3 methods, and I want you help me to confirm:</p>
  
  <h2>Using Microsoft.Jet.OLEDB.4.0, like this:</h2>
  
  <pre><code>INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
                         'Text;Database=C:\Temp\;HDR=Yes;',
                         'SELECT * FROM test.csv')
              (object_id, name)
  SELECT object_id, name
    FROM sys.tables;
  </code></pre>
  
  <p><strong>but this need the csv file is there, and with header</strong></p>
  
  <h2>using SQLCMD</h2>
  
  <p>command line. </p>
  
  <h2>using BCP</h2>
  
  <p>Use union, get data and it's column header. </p>
  
  <p>This is all my understanding about T-SQL export to CSV, please help me to confirm.</p>
  
  <p>Is there other way to export to CSV? </p>
  
  <p>Thanks!</p>