title: How to run a stored procedure in a loop
body: <p>I have a table for <code>"Departments" (ID,deptID)</code></p>
  
  <pre><code>ID  parentdeptID  
  2 null  
  3 null  
  7 2  
  8 2  
  9 3  
  </code></pre>
  
  <p>and a second table <code>"Filter"</code> contains IDs only  </p>
  
  <pre><code>ID  
  2  
  9  
  </code></pre>
  
  <p>and a stored procedure with only input prameter <code>@deptID</code> and the output is a result from joining some of tables depending on the input ID</p>
  
  <p>my question is: a way read from table "Filter" and gets IDs, and for each ID get its childs if it has<br>
  in our example:</p>
  
  <p><code>2,7,8,9 (2,9  from "Filter" and 7,8 from "Departments" as childs of 2)</code></p>
  
  <p>and then for each one of this 4 IDs excute the stored procedure by ID  </p>
  
  <p>This is the overview, if any one has a better way without using stored procedure he is welcome</p>