I'm trying to recursively loop through and return all child_id
's that have the root element of 9
.
The structure:
+-- 9
| +-- 8
| +-- 17
| +-- 33
| +-- 18
| +-- 22
| +-- 11
| +-- 4
Parent Child Link Table: (table name: elements_children)
+----+-----------+----------+
| id | parent_id | child_id |
+----+-----------+----------+
| 1 | 9 | 8 |
| 2 | 8 | 17 |
| 3 | 8 | 33 |
| 4 | 8 | 18 |
| 5 | 9 | 22 |
| 6 | 22 | 11 |
| 7 | 22 | 4 |
| 8 | 3 | 5 |
+----+-----------+----------+
The Desired Output - [8,17,33,18,22,11,4]
Procedure 1 (getChildren):
BEGIN
-- CREATE TEMP TABLE
DROP TABLE IF EXISTS total_children;
CREATE TEMPORARY TABLE total_children(
id INT(11) NOT NULL auto_increment PRIMARY KEY,
child_id VARCHAR(255)
);
-- CALL TO PROCEDURE 2
CALL getNodes(rootNode);
-- SELECT child_id's FROM the temp table, then drop the table
SELECT child_id FROM total_children;
DROP TABLE total_children;
END
Procedure 2 (getNodes):
BEGIN
-- VARIABLES
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE childNode VARCHAR(255);
-- CURSOR1
DECLARE cur1 CURSOR FOR SELECT child_id FROM elements_children WHERE parent_id = parentNode;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
-- START LOOP
myloop:LOOP
FETCH cur1 INTO childNode;
-- CHECK IF DONE IS TRUE
IF(done) THEN
LEAVE myloop;
END IF;
-- APPEND TO TOTAL
INSERT INTO total_children(child_id) SELECT childNode;
-- RECURSIVE
CALL getNodes(childNode);
END LOOP myloop;
-- END LOOP
-- END CURSOR1
CLOSE cur1;
END
I'm getting error: recursion limit exceeded 200
I set the recursion limit to 200 and I know the procedure shouldn't recurse 200 times so there has to be an error in my code that isn't stopping the recursion, I believe something to do with the done
variable in the myloop: LOOP
.
Question: Why is my procedure generating this recursion error?