title: Export SQL Server query to CSV using script
body: <p>Let say I have 1 query for remote SQL Server:</p>
  
  <pre><code>SELECT * FROM USER
  </code></pre>
  
  <p>And I would like to export using any kind of script without using manual intervention from SSMS. I expect the output to be like this:</p>
  
  <pre><code>id,username,firstName,lastName
  1,amy,Amy,NULL
  2,bob,Bob,Marley
  3,cat,Cathy,NULL
  </code></pre>
  
  <p>How can I export into something like above? </p>
  
  <hr>
  
  <p>I noticed there are few old discussions on this with variety of custom workaround that doesn't work for me. I prefer the exported file contains header, NULL value and without start/end quotes. Here's my closest script to achieve the result:</p>
  
  <pre><code>bcp "SELECT * FROM USER;" queryout export.csv -S 'localhost' -U sa -P 'password' -d 'mydatabase' -c -t  ',' 
  </code></pre>
  
  <p>Current output:</p>
  
  <pre><code>1,amy,Amy,
  2,bob,Bob,Marley
  3,cat,Cathy,
  </code></pre>