title: How to use Loop Stored procedure in SQL Server?
body: <p>I have a classes table in which I need to run stored procedure (SQL Server) in loop of class i.e., it should run 5 times here because maximum class in 5th standard in table. On every loop status field should update as pass or fail.</p>
  
  <p>Kind of table is mentioned below.</p>
  
  <p>Table Name: <code>classes</code></p>
  
  <p><a href="https://i.stack.imgur.com/EVkW7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EVkW7.png" alt="enter image description here"></a></p>
  
  <p>Stored procedure:</p>
  
  <pre><code>DECLARE @counter INT = 1;
  
  SELECT count(class)
  FROM classes
  GROUP BY class;
  
  WHILE @counter &lt;= * * [here I need count of distinct class] * *;//i.e, here 5 
  BEGIN
      PRINT @counter;
  
      UPDATE classes
      SET STATUS = "Pass"
      WHERE class = * * [here I need class value] * *;
  
      SET @counter = @counter + 1;
  END
  </code></pre>
  
  <p>Please help me to run this stored procedure.</p>