写了一个mysql的函数,但是运行出错,哪位帮我看看。谢谢!!!很急!!! 5
//createordropfunctiongroupLst(rootidvarchar(25))//returnsvarchar(1000)//begin//setma...
//create or drop function groupLst(rootid varchar(25))
//returns varchar(1000)
// begin
// set max_sp_recursion_depth=225;
// declare idStr VARCHAR(1000);
// declare chID VARCHAR(1000);
// set idStr = '';
// set chID =rootid;
// while chID is not null do
// set idStr = concat(idStr,',',chID);
// select group_concat(id) into chID from groups where FIND_IN_SET(parentID,chID)>0;
// end while;
// return idStr;
// end; 展开
//returns varchar(1000)
// begin
// set max_sp_recursion_depth=225;
// declare idStr VARCHAR(1000);
// declare chID VARCHAR(1000);
// set idStr = '';
// set chID =rootid;
// while chID is not null do
// set idStr = concat(idStr,',',chID);
// select group_concat(id) into chID from groups where FIND_IN_SET(parentID,chID)>0;
// end while;
// return idStr;
// end; 展开
3个回答
2013-07-17
展开全部
由于没有你的表结构, 我另外创建了一个表
CREATE TABLE test_tree (
test_id INT NOT NULL,
pid INT,
test_val VARCHAR(10),
PRIMARY KEY (test_id)
);
INSERT INTO test_tree VALUES(1, NULL, '.NET');
INSERT INTO test_tree VALUES(2, 1, 'C#');
INSERT INTO test_tree VALUES(3, 1, 'J#');
INSERT INTO test_tree VALUES(4, 1, 'ASP.NET');
INSERT INTO test_tree VALUES(5, 1, 'VB.NET');
INSERT INTO test_tree VALUES(6, NULL, 'J2EE');
INSERT INTO test_tree VALUES(7, 6, 'EJB');
INSERT INTO test_tree VALUES(8, 6, 'Servlet');
INSERT INTO test_tree VALUES(9, 6, 'JSP');
INSERT INTO test_tree VALUES(10, NULL, 'Database');
INSERT INTO test_tree VALUES(11, 10, 'DB2');
INSERT INTO test_tree VALUES(12, 10, 'MySQL');
INSERT INTO test_tree VALUES(13, 10, 'Oracle');
INSERT INTO test_tree VALUES(14, 10, 'SQL Server');
INSERT INTO test_tree VALUES(15, 13, 'PL/SQL');
INSERT INTO test_tree VALUES(16, 15, 'Function');
INSERT INTO test_tree VALUES(17, 15, 'Procedure');
INSERT INTO test_tree VALUES(18, 15, 'Package');
INSERT INTO test_tree VALUES(19, 15, 'Cursor');
INSERT INTO test_tree VALUES(20, 14, 'T-SQL');
修改后的函数代码
DELIMITER //
create function groupLst(rootid varchar(25))
returns varchar(1000)
begin
declare idStr VARCHAR(1000);
declare chID VARCHAR(1000);
set max_sp_recursion_depth=225;
set idStr = '';
set chID =rootid;
while chID is not null do
set idStr = concat(idStr,',',chID);
select group_concat(test_id) into chID from test_tree where FIND_IN_SET(pid, chID)>0;
end while;
return idStr;
end;
//
DELIMITER ;
执行结果:
mysql> select groupLst(1);
+-------------+
| groupLst(1) |
+-------------+
| ,1,2,3,4,5 |
+-------------+
1 row in set (0.06 sec)
mysql>
mysql> select groupLst(6);
+-------------+
| groupLst(6) |
+-------------+
| ,6,7,8,9 |
+-------------+
1 row in set (0.00 sec)
修改点, 你那里的
create or drop function
我修改成了
create function
你那里的
set max_sp_recursion_depth=225;
declare idStr VARCHAR(1000);
declare chID VARCHAR(1000);
我修改成了
declare idStr VARCHAR(1000);
declare chID VARCHAR(1000);
set max_sp_recursion_depth=225;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码有误。。在仔细检查pfmrwy
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询