DELIMITER $$
DROP PROCEDURE IF EXISTS `changelevel`$$
CREATE PROCEDURE `changelevel`()
changelevel:BEGIN
DECLARE cpath TEXT;
DECLARE ctype CHAR(1);
DECLARE ccount INT;
DECLARE lastrow int;
DECLARE cursorlevels CURSOR FOR
SELECT count(*), `path`, `type`
FROM `users`
GROUP BY `path`, `type`;
DECLARE CONTINUE HANDLER FOR 1329 SET lastrow=1;
SET lastrow = 0;
OPEN cursorlevels;
loop1:LOOP
FETCH cursorlevels INTO ccount, cpath, ctype;
IF lastrow = 1 THEN
leave loop1;
END IF;
UPDATE `users`
SET level = ccount
WHERE path = cpath and type = ctype;
END LOOP loop1;
close cursorlevels;
LEAVE changelevel;
END$$
DELIMITER ;