title: How to run SQL query repeatedly (in loop) with just two parameters changing for each run
body: <p>I have a database table "table_name" in SQL Server 2012 and I am using SQL Server Management Studio version 10. </p>
  
  <p>Data in table looks like:</p>
  
  <pre><code>id          create_date     update_date     time_generated  kpname                      machine_name value_type     value
  10706652    6/23/16 1:08 PM 6/23/16 1:08 PM 6/23/16 1:00 PM Win-RT7_Abc Onetwo_Location BRAABBCCZ02 response_time   18.015
  10707166    6/23/16 1:13 PM 6/23/16 1:13 PM 6/23/16 1:05 PM Win-RT7_Abc Onetwo_Location BRAABBCCZ02 response_time   18.313
  10707663    6/23/16 1:18 PM 6/23/16 1:18 PM 6/23/16 1:10 PM Win-RT7_Abc Onetwo_Location BRAABBCCZ02 response_time   18
  10708157    6/23/16 1:23 PM 6/23/16 1:23 PM 6/23/16 1:15 PM Win-RT7_Abc Onetwo_Location BRAABBCCZ02 response_time   18.328
  </code></pre>
  
  <p>I have a dirty query that pulls data for past 6 weeks, for same day of the week and same time window of the day. The query is:</p>
  
  <pre><code>--wk1
  select *
  from table_name
  where time_generated &gt;= '2016-06-16 13:17:30'
    and time_generated &lt;= '2016-06-16 13:22:29'
    and machine_name like 'BRAABBCCZ0%' 
    and kpname like '%Win-RT7_Abc Onetwo_%'
    and value_type = 'response_time'
  
  union
  
  --wk2
  select *
  from table_name
  where time_generated &gt;= '2016-06-09 13:17:30'
    and time_generated &lt;= '2016-06-09 13:22:29'
    and machine_name like 'BRAABBCCZ0%' 
    and kpname like '%Win-RT7_Abc Onetwo_%'
    and value_type = 'response_time'
  
  union
  
  --wk3
  select *
  from table_name
  where time_generated &gt;= '2016-06-02 13:17:30'
    and time_generated &lt;= '2016-06-02 13:22:29'
    and machine_name like 'BRAABBCCZ0%' 
    and kpname like '%Win-RT7_Abc Onetwo_%'
    and value_type = 'response_time'
  
  union
  
  --wk4
  select *
  from table_name
  where time_generated &gt;= '2016-05-26 13:17:30'
    and time_generated &lt;= '2016-05-26 13:22:29'
    and machine_name like 'BRAABBCCZ0%' 
    and kpname like '%Win-RT7_Abc Onetwo_%'
    and value_type = 'response_time'
  
  union
  
  --wk5
  select *
  from table_name
  where time_generated &gt;= '2016-05-19 13:17:30'
    and time_generated &lt;= '2016-05-19 13:22:29'
    and machine_name like 'BRAABBCCZ0%' 
    and kpname like '%Win-RT7_Abc Onetwo_%'
    and value_type = 'response_time'
  
  union
  
  --wk6
  select *
  from table_name
  where time_generated &gt;= '2016-05-12 13:17:30'
    and time_generated &lt;= '2016-05-12 13:22:29'
    and machine_name like 'BRAABBCCZ0%' 
    and kpname like '%Win-RT7_Abc Onetwo_%'
    and value_type = 'response_time'
  </code></pre>
  
  <p>Is there any way I can run this same query (may be in loop or something) for different values of "time_generated"? For example I want to run this query 12 times to get data between 13:17:30 to 14:17:29 over five minute windows.</p>
  
  <p>So I want to run above query 12 times for following values of time generated (date part will remain same, only time part will change)</p>
  
  <pre><code>13:17:30    13:22:29
  13:22:30    13:27:29
  13:27:30    13:32:29
  13:32:30    13:37:29
  13:37:30    13:42:29
  13:42:30    13:47:29
  13:47:30    13:52:29
  13:52:30    13:57:29
  13:57:30    14:02:29
  14:02:30    14:07:29
  14:07:30    14:12:29
  14:12:30    14:17:29
  </code></pre>
  
  <p>Please suggest how I can do this and apologies for a lengthy (and probably confusing) question. I tried my best to describe it clearly.</p>