USE `vn`; DROP procedure IF EXISTS `department_calcTree`; DELIMITER $$ USE `vn`$$ CREATE DEFINER=`root`@`%` PROCEDURE `department_calcTree`() BEGIN /** * Calculates the #path, #lft, #rgt, #sons and #depth columns of * the #department table. To build the tree, it uses the #parentFk * column. */ DECLARE vIndex INT DEFAULT 0; DECLARE vSons INT; DROP TEMPORARY TABLE IF EXISTS tNestedTree; CREATE TEMPORARY TABLE tNestedTree SELECT id, path, lft, rgt, depth, sons FROM department LIMIT 0; SET max_sp_recursion_depth = 5; CALL department_calcTreeRec(NULL, '/', 0, vIndex, vSons); SET max_sp_recursion_depth = 0; UPDATE department z JOIN tNestedTree t ON t.id = z.id SET z.path = t.path, z.lft = t.lft, z.rgt = t.rgt, z.depth = t.depth, z.sons = t.sons; DROP TEMPORARY TABLE tNestedTree; END$$ DELIMITER ;