title: How to iterate column ID independently in while loop SQL Server
body: <p>In this example, salary and bonus, from different employee is mixed in while loop, which make the calculation invalid. </p>
  
  <p>How to iterate each ID independently and do certain calculation based on that ID?</p>
  
  <pre><code>while (select count(*) from db.employee) &gt; 0
  begin   
      select top 1 ID, SALARY, [month] 
      from db.employee  
      order by [month] asc
  
      if deserve = yes
      begin
          set SALARY = SALARY + (bonus * 2)   
      end
  end
  </code></pre>
  
  <p>This is not my script. I have complex script, that will take time to understand the objective. However, I mimic the logic to this simple idea. So later I can tweak my original script based on suggestion. The original one is <a href="https://stackoverflow.com/questions/47820935/iterate-through-tables-and-do-calculation-based-on-one-column-sql-server?noredirect=1#comment82666901_47820935">Iterate through tables and do calculation based on one column SQL server</a></p>