title: SQL Server FOR EACH Loop
body: <p>I have the following SQL query:</p>
  
  <pre><code>DECLARE @MyVar datetime = '1/1/2010'    
  SELECT @MyVar
  </code></pre>
  
  <p>This naturally returns '1/1/2010'.</p>
  
  <p>What I want to do is have a list of dates, say:</p>
  
  <pre><code>1/1/2010
  2/1/2010
  3/1/2010
  4/1/2010
  5/1/2010
  </code></pre>
  
  <p>Then i want to FOR EACH through the numbers and run the SQL Query.</p>
  
  <p>Something like (pseudocode):</p>
  
  <pre><code>List = 1/1/2010,2/1/2010,3/1/2010,4/1/2010,5/1/2010
  
  For each x in List
  do
    DECLARE @MyVar datetime = x
  
    SELECT @MyVar
  </code></pre>
  
  <p>So this would return:-</p>
  
  <p>1/1/2010
      2/1/2010
      3/1/2010
      4/1/2010
      5/1/2010</p>
  
  <p>I want this to return the data as one resultset, not multiple resultsets, so I may need to use some kind of union at the end of the query, so each iteration of the loop unions onto the next.</p>
  
  <p><strong>edit</strong></p>
  
  <p>I have a large query that accepts a 'to date' parameter, I need to run it 24 times, each time with a specific to date which I need to be able to supply (these dates are going to be dynamic) I want to avoid repeating my query 24 times with union alls joining them as if I need to come back and add additional columns it would be very time consuming.</p>