I have following table structure
id | Name | senior id
1 | superadmin | -1
2 | abc | 1
3 | xyz | 1
4 | pqr | 2
5 | vwx | 3
6 | stu | 3
7 | efg | 4
8 | ijk | 3
9 | nop | 3
10 | rst | 3
11 | uvw | 9
Here I Wrote a stored procedure which takes id as a input and will return a string of concatanated names seperated by '/' from the name associated with id to the name whose seniorid is -1.
for example if I pass the id as 10 to SP I will expect the return string as
rst/xyz/superadmin/
Here is the stored procedure that I have written
DELIMITER $$;
DROP PROCEDURE IF EXISTS `mydb`.`get_list_up_to_senior`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `get_list_up_to_senior`(id int (8))
BEGIN
DECLARE strsenior VARCHAR(250);
DECLARE strseniorlist VARCHAR(250);
DECLARE temp int(8);
SET strsenior = '';
SET strseniorlist = '';
SET temp = id;
WHILE temp != -1 DO
SET strsenior = (SELECT name FROM mydb.tblname WHERE id = temp limit 1);
SET temp = (SELECT seniorid FROM mydb.tblname WHERE id = temp limit 1);
SET strseniorlist = CONCAT(strseniorlist,strsenior,'/');
END WHILE;
SELECT strseniorlist;
END$$
DELIMITER ;$$
When I compiles this SP I get compiles with no error. But when I execute this SP; its get executed perfectly and returns
strseniorlist
superadmin/
1)What is wrong in my SP so that I am not getting desired output.
My database version is MySQL 6.0 and I am using Sqlyog to execute above SP.
2)When I execute the above SP on MySql 5.0 Its not get created and gives following errors
ERROR :
(0 row(s) affected)
(0 ms taken)
Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'PROCEDURE `get_list_up_to_senior`(id int (8))
BEGIN
DECLARE strsenior VA' at line 1
(16 ms taken)
Please guide me friends in this to solve the problems Thank You!