0

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!

4

1 回答 1

0

我找到了原因并回答了我的第一个问题。未获得预期输出的原因是我使用用户定义变量名“id”作为 SP 的传入参数。这个变量名“id”类似于我提到的表的列名。所以 MySql 将这个 'id' 视为局部变量而不是表列。冲突就在这里。如果您将传入参数变量名称更改为 SP 到其他不会与查询中提到的表中的列名冲突的名称。您将获得所需的输出。

谢谢你!

于 2012-10-15T13:30:47.377 回答