title: How to run a query multiple times with different parameters?
body: <p>I'm trying to figure out the best way to get a query to run multiple times with different parameters. I've tried putting it as a stored procedure and running it with cursors, but I'm pretty novice at the cursor concept. Here is the query and my first attempt with cursor.</p>
  
  <pre><code>SELECT 
  AVG([processingseconds])
  FROM [nucor_historical_data].[dbo].[test_Lift_Matrix]
  Where ActualGauge between 0 and .21875 and ActualWidth between 0 and 55
  and inches between   0 and 120 and MaxLiftWeight between 0 and 10000 and
  processingseconds is not null
  </code></pre>
  
  <p>So the parameters I need to loop through are in the where statement. I have combinations for all these groupings you see in another table.</p>
  
  <p>someone suggested trying this to me earlier from another stack question, so I tested with one parameter but couldn't get it working. Is there a better way to attempt this?</p>
  
  <pre><code>DECLARE @param varchar(200)
  
  -- getting your parameter from the table
  DECLARE curs CURSOR LOCAL FAST_FORWARD FOR
  SELECT gauge FROM groupings
  
  OPEN curs
  
  FETCH NEXT FROM curs INTO @param
  
  -- executing your stored procedure once for every value of your parameter     
  WHILE @@FETCH_STATUS = 0 BEGIN
  EXEC group_average @param
  FETCH NEXT FROM curs INTO @param
  END
  
  CLOSE curs
  DEALLOCATE curs
  </code></pre>