title: Can't get this WHILE LOOP working in MySQL
body: <p>I'm new to loops in Mysql and there doesn't seem to be that much info online about them. I have searched al over and can't get anything to work. Can anyone help with this please, this is the code </p>
  
  <pre><code>PROCEDURE while_loop()
    BEGIN
  
      SET @var_number_of_departments = (SELECT COUNT(DISTINCT departmentid) FROM employees); -- finds number of departments
      SET @var_departmentid = 1; -- sets incrementing value to 1
  
      WHILE @var_number_of_departments &gt; @var_departmentid DO -- loop until all departments have been analyzed                            
  
        SELECT Name FROM employees WHERE BossID IS NULL AND departmentid =  @var_departmentid AND salary &gt; (SELECT salary     FROM employees WHERE bossID IS NOT NULL    AND DepartmentID = @var_departmentid);
  
        SET @var_departmentid = @var_departmentid + '1'; -- increment value in order to analyze the next department
  
      end while;                           
  end;
  </code></pre>