这是我第一次编写 MySQL 存储函数,但我遇到了问题,看看我做错了什么:
这是我的表格和功能:
mysql> select * from PathNames;
+-----+--------+-----+
| NID | Name | PID |
+-----+--------+-----+
| 1 | / | 1 |
| 6 | US | 1 |
| 7 | TX | 6 |
| 8 | Dallas | 7 |
| 9 | Plano | 7 |
| 10 | Frisco | 7 |
+-----+--------+-----+
DROP FUNCTION IF EXISTS NID2PathName;
DELIMITER //
CREATE FUNCTION NID2PathName(nid INT UNSIGNED)
RETURNS TEXT DETERMINISTIC
BEGIN
DECLARE rtn TEXT;
DECLARE str TEXT;
DECLARE tmp TEXT;
DECLARE id INT UNSIGNED;
DECLARE pid INT UNSIGNED;
SET id = nid;
SET rtn = "";
SET str = "";
SET tmp = "";
WHILE id > 1 DO
SELECT Name, PID into str, pid FROM PathNames WHERE NID=id LIMIT 1;
SET rtn = CONCAT('/', str, rtn);
set id = pid;
END WHILE;
RETURN rtn;
END//
DELIMITER ;
SELECT NID2PathName(10);
我期望SELECT NID2PathName(10)
返回"/US/TX/Frisco"
,但我只得到"//"
。任何帮助将不胜感激。