title: How does one use loops in TSQL?
body: <p>In TSQL, I would like to change the following code from have to use hard coded dhomes to using a loop for optimization. My failed attempt at trying to add a loop is also included.</p>
  
  <pre><code>Declare  @dhome Tinyint, @bp smallint, @lr smallint, @q smallint
  
      // Set @dhome = 1
    While(@dhome &lt;= 3) // My attempt to add a loop
  
    SELECT @lr = MAX(NQdDate), @q = NQd
    FROM NQdHistory
    WHERE dhomeId = @dhome 
    GROUP BY NQdDate, NQd
  
    SELECT @bd = COUNT(*)
    FROM bdhome
    WHERE NQdDate= @lr AND dhomeID= @dhome 
  
    DELETE FROM ND1 WITH(XLOCK)
    WHERE dhomeID= @dhome  AND NQdDate= @lr
  
    UPDATE NQdHistory
    SET Nbd = @q - @@RowCount - @bp, NBd = @bp
    WHERE NQdDate= @lr AND dhomeID= @dhome 
  
    Set @dhome = @dhome +1 //My attempt to end a loop
  </code></pre>