hello i have table profile that has many attributs, and the profile has on parent but the parent can have a parent etc..
I want to get all the list of the attributs(even the attributs of the parents)
i wrote this stored procedure with mysql 5.1.42 but it returns only the attributs of the profile without including the attributs of the parents
…
CREATE DEFINER=`root`@`localhost` PROCEDURE `get_all_attributes`(IN profile_id INT)
BEGIN
DECLARE parent_id INT DEFAULT NULL;
-- Get the attributes for the current profile
SELECT
Id, attribut_rank, attribut_type, create_date, editor, label, memo, name, profile
FROM
marketing_profile_attribut
WHERE
profile = profile_id
ORDER BY
attribut_rank;
-- Get the parent ID for the current profile
SELECT
parent
INTO
parent_id
FROM
marketing_profile
WHERE
Id = profile_id;
-- If the parent ID is not null, call the stored procedure recursively with the parent ID
IF parent_id IS NOT NULL THEN
CALL get_all_attributes(parent_id);
END IF;
END
...
please can you tell me what is wrong with this procedure i am working with mysql 5.1.42
2
Answers
Check the MySQL’s
max_sp_recursion_depth
setting. As you are only looking for direct parents from the tree you can use a cursor loop instead of the recursion.The best solution is to update your old MySQL version to a newer one and you can use common table expressions for that.
There are several things that make the procedure doesn’t work properly.
Considering the two points abobe the procedure could be something like this: