title: MySQL stored procedure error: unknown system variable
body: <p>I have a problem with a stored procedure in MySQL 5.5. This is the table on which the procedure must work:</p>
  
  <pre><code>Table `diba`:
  ----------------------------------------------------
  Column   | Type    | Null | Default | Links to
  ----------------------------------------------------
  ParentID | int(11) | No   |         | articoli -&gt; id         
  ChildID  | int(11) | No   |         | articoli -&gt; id
  Quantity | int(11) | No   |
  lvl      | int(11) | No   | 0            
  </code></pre>
  
  <p>and this is the procedure itself:</p>
  
  <pre><code>DELIMITER //
  CREATE PROCEDURE RenumberLevels()
  DETERMINISTIC
  BEGIN
  DECLARE lvl_counter INT;
  SET lvl_counter = 1;
  UPDATE `diba` SET `diba`.`lvl` = CASE WHEN `diba`.`ParentID` IS NULL THEN 1 ELSE 0 END;
  WHILE EXISTS (SELECT * FROM `diba` WHERE `diba`.`lvl` = 0) DO
  UPDATE `diba` SET `diba`.`lvl` = lvl_counter +1 WHERE (SELECT `D2`.`lvl` FROM `diba` AS D2
  WHERE D2.ChildID = `diba`.`ParentID`) &gt; 0 AND `diba`.`lvl` = 0;
  SET `diba`.`lvl` = lvl_counter + 1;
  END WHILE;
  END//
  DELIMITER ;
  </code></pre>
  
  <p>The resulting error is:
  <code>#1193 - Unknown system variable 'lvl'</code></p>
  
  <p>What's wrong with this procedure?</p>