title: Loop in teradata
body: <p>I've tried googling but I just end up in pages about BTEQ so:</p>
  
  <p>How do you create a simple loop in teradata? Can I do this?</p>
  
  <p>Say that I want to query everything from 20 different tables that are named dw_table1, dw_table2, dw_table3, dw_table4, dw_table5 /.../ dw_table20 to send export the output to excel (by using the export option in teradata, so forgo that). I.e. I want to (with a mix of matlab syntax)</p>
  
  <pre><code>for i = 1:20           
      SELECT * FROM dw_table(i)
  end
  </code></pre>
  
  <p>Where i = 1:20 is the range of the loop and dw_table(i) is the specific i. So it would query * from table1 and then more to table2 and continue until table20. </p>
  
  <p>Or, lets assume that I want to do the same query as above but with a where condition that includes a loop in itself. This would then for table1 firstly send the query for column2 between 1000 to 2000, then for 2000 to 3000, then for 3000 to 4000 and so on. When it completes 9000 to 10000 the inner loop stops and the outer loop changes to to table2 and the process of the inner loop repeats. This continues for all 20 tables and for all the intervals. </p>
  
  <pre><code>for i = 1:20
      for j = 1:1000:10000
          SELECT * FROM dw_table(i)
          Where column2 between j and j+1000
      end
  end
  </code></pre>
  
  <p>Where j controls the interval on column2
  Thanks </p>