DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `pro_cre_childlist`(IN rootId varchar(50),IN nDepth int, IN vdesc varchar(45))
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE b varchar(50);
DECLARE cur1 CURSOR FOR SELECT loginname FROM users WHERE rid=rootId;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
SET max_sp_recursion_depth=15;
INSERT INTO tmpLst VALUES (NULL,rootId,nDepth,vdesc);
OPEN cur1;
FETCH cur1 INTO b;
WHILE done=0 DO
CALL pro_cre_childlist(b,nDepth+1,vdesc);
FETCH cur1 INTO b;
END WHILE;
CLOSE cur1;
END$$
DELIMITER ;
原文:http://www.cnblogs.com/zero530/p/5118929.html