我试图使用以下过程在MySQL中检索逗号分隔的层次结构字符串:
DROP PROCEDURE IF EXISTS `getAccessibleEntIdz`;
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `getAccessibleEntIdz`(IN entId VARCHAR(20), OUT accessibleEntIds VARCHAR(20))
BEGIN
DECLARE DONE INT DEFAULT FALSE;
DECLARE tEntId VARCHAR(20) DEFAULT '';
DECLARE tParentEntId VARCHAR(20) DEFAULT '';
DECLARE tCursor CURSOR FOR SELECT ent_id, ent_parent_id FROM ent_mst WHERE ent_parent_id = entId;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET DONE = TRUE;
OPEN tCursor;
readLoop : LOOP
FETCH tCursor INTO tEntId, tParentEntId;
IF DONE THEN
LEAVE readLoop;
END IF;
SET accessibleEntIds = CONCAT(accessibleEntIds,",",tEntId);
CALL getAccessibleEntIdz(tEntId, accessibleEntIds);
END LOOP;
CLOSE tCursor;
END$$
DELIMITER ;
但我收到的输出是NULL。我正在以下列方式打电话:
SET @@GLOBAL.max_sp_recursion_depth = 255;
SET @@session.max_sp_recursion_depth = 255;
SET @accessibleEntIds = '';
CALL `getAccessibleEntIdz`('1', @accessibleEntIds);
SELECT @accessibleEntIds;
我尝试通过将SELECT命令打印到中间打印变量值来调试,发现问题是accessibleEntIds变量无法维持调用之间的值。
有没有什么方法可以维持调用之间的值,比如全局变量,或者可能就像C语言中的静态变量一样,或者我走向错误的方向,还有其他更简单的方法,请帮助,我是新手写程序!!
注意:我正在使用的表结构:
CREATE TABLE `ent_mst` (
`ENT_ID` varchar(40) NOT NULL,
`ENT_PARENT_ID` varchar(40) DEFAULT NULL,
`ENT_NAME` varchar(200) NOT NULL,
`ENT_URL` varchar(200) DEFAULT NULL,
`ENT_LOGO_PATH` varchar(256) DEFAULT NULL,
`CREATED_DATE_TIME` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`UPDATED_DATE_TIME` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`ENT_ID`),
KEY `ENT_PARENT_ID` (`ENT_PARENT_ID`),
CONSTRAINT `ent_mst_ibfk_1` FOREIGN KEY (`ENT_PARENT_ID`) REFERENCES ent_mst (`ENT_ID`)
);
insert into iot_ent_mst values ('1',NULL,'A','url1','loc1',null,null),
('2','1','B','url2','loc2',null,null), ('3','1','C','url5','loc5',null,null),
('4','3','D','url3','loc3',null,null),('5','1','E','url4','loc4',null,null);