I have a problem with a stored procedure in MySQL 5.5. This is the table on which the procedure must work:
Table `diba`:
----------------------------------------------------
Column | Type | Null | Default | Links to
----------------------------------------------------
ParentID | int(11) | No | | articoli -> id
ChildID | int(11) | No | | articoli -> id
Quantity | int(11) | No |
lvl | int(11) | No | 0
and this is the procedure itself:
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`) > 0 AND `diba`.`lvl` = 0;
SET `diba`.`lvl` = lvl_counter + 1;
END WHILE;
END//
DELIMITER ;
The resulting error is:
#1193 - Unknown system variable 'lvl'
What's wrong with this procedure?