code:
DELIMITER $$
CREATE
/*[DEFINER = { user | CURRENT_USER }]*/
FUNCTION `temp`.`findRootEle`
(in_ele VARCHAR(255),
in_pele VARCHAR(255))
RETURNS VARCHAR(255)
/*LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT 'string'*/
BEGIN
DECLARE l_rid VARCHAR(255);
DECLARE l_prid VARCHAR(255);
SET l_rid=in_ele;
SET l_prid=in_pele;
WHILE l_prid <> "1" DO
SET l_rid=l_prid;
SELECT parent_id INTO l_prid FROM region WHERE id=l_rid;
END WHILE;
RETURN(l_rid);
END$$
DELIMITER ;