title: Write T-SQL Result Lines to Individual Files
body: <p>I am working on a proprietary data warehousing product that has a metadata table containing the SQL queries used to populate it. I need to extract each of these queries from the metadata table and save it to its own .sql file, named according to the table it populates.</p>
  <p>There are about 400 of these queries, and I'd like to automate it, but I don't know a good way to do that. Is there a good way to iterate across the result set, line-by-line, and write the output to a file?</p>
  <p>An example of what I'm trying to do. Let's say I returned two lines in my result set:</p>
  <p><code>select tableName, extractQuery from packages;</code></p>
  <blockquote>
  <p>tableName extractQuery</p>
  <p>Table1 select * from sourceTable1;</p>
  <p>Table2 select * from sourceTable2;</p>
  </blockquote>
  <p>Then I would need this to be saved into two distinct files, Table1.sql and Table2.sql, each containing the returned SQL command.</p>