title: MS SQL Server 2008 R2 Express Script to Write Query to Text file
body: <p>I have a query whose results I'd like to write to a text file.  I'm doing this across several Db's and output files and so would like to automate it. </p>
  
  <p>I know how to do this in <code>SSMS</code> by running the script with the <code>output</code> set to <code>file</code> but was curious if there is a way to do it in a script.</p>
  
  <p>Ideally something like :</p>
  
  <pre><code>SELECT * FROM tablel
  OUTPUT TO 'c:\myFile1.txt'    
  
  SELECT * FROM table2
  OUTPUT TO 'c:\myFile2.txt'    
  
  SELECT * FROM table3
  OUTPUT TO 'c:\myFile3.txt'
  </code></pre>
  
  <p>Any suggestions?</p>