title: sql server: how to execute a .sql from another query?
body: <p>I have a .sql script with a lot of action queries that work on some staging tables.  This script needs to be run twice with some other commands in-between i.e.:</p>
  <ol>
  <li>Load the staging table from source A</li>
  <li>Use <code>do_stuff.sql</code> to process it</li>
  <li>Move the results somewhere.</li>
  <li>Repeat Steps 1-3 for source B.</li>
  </ol>
  <p>The brute force approach would be to just copy &amp; paste dostuff.sql as needed.  While this would technically work, is there a better way?</p>
  <p>I'm hoping there's a command like <code>RunThisSQL 'C:\do_stuff.sql'</code> that I haven't discovered yet.</p>
  <h3>Update</h3>
  <p>Well, it's been about 5 years and I just re-discovered this old question.  I did this recently and made a cursor to loop thru a master table.  For each record in that master table, the script runs through an inner script using variables set by the master table.</p>
  <p><a href="https://www.mssqltips.com/sqlservertip/1599/sql-server-cursor-example/" rel="nofollow noreferrer">https://www.mssqltips.com/sqlservertip/1599/sql-server-cursor-example/</a></p>