oracle递归:MySQL使用递归存储过程实现树状结构 实现Oracle的Connect By 功能来源: 发布时间:星期四, 2009年2月12日 浏览:51次 评论:0
1 创建个数据表 查看复制到剪切板打印 CREATE TABLE `tree` ( `id` (10) NOT NULL, `fid` (10) NOT NULL ); id 是编号 fid 是上级编号 顶层是0或者你自己定义 2 创建临时表 create temporary table not exists tmp_table(id big(20),fid big(20),lvl )// 3 编写存储过程 查看复制到剪切板打印 DELIMITER // drop procedure exists useCursor // -- 建立存储过程 -- JAVA世纪网 <a target="_blank" href="www.java2000.net">www.java2000.net</a> 老紫竹 CREATE PROCEDURE useCursor(iid big(20),lvl ) BEGIN -- 局部变量定义 declare tid big(20) default -1 ; declare tfid big(20) default -1 ; -- 游标定义 declare cur1 CURSOR FOR select id,fid from tree where fid=iid ; -- 游标介绍定义 declare CONTINUE HANDLER FOR SQLSTATE '02000' SET tid = null,tfid=null; SET @@max_sp_recursion_depth = 10; -- 开游标 OPEN cur1; FETCH cur1 INTO tid,tfid; WHILE ( tid is not null ) DO insert o tmp_table values(tid,tfid,lvl); -- 树形结构数据递归收集到建立临时表中 call useCursor(tid,lvl+1); FETCH cur1 INTO tid,tfid ; END WHILE; END;// DELIMITER ; 4 方式并删除临时表 call useCursor(2,0); select * from tmp_table ; 5 删除临时表 drop temporary table exists tmp_table ; 0
相关文章读者评论发表评论 |
|