MySQL中怎么实现存储过程递归调用

发布时间:2021-08-13 17:05:05 作者:Leah
来源:亿速云 阅读:212

今天就跟大家聊聊有关MySQL中怎么实现存储过程递归调用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

 
有分类表tb_system_category,结构如下:
[java]  
CREATE TABLE `tb_system_category` (  
  `id` int(11) NOT NULL AUTO_INCREMENT,  
  `c_parent_id` int(11) NOT NULL,  
  `c_name` varchar(50) NOT NULL,  
  `c_full_name` varchar(200) DEFAULT NULL,  
  `c_code` varchar(50) NOT NULL,  
  `c_describe` text,    www.2cto.com  
  PRIMARY KEY (`id`)  
) ENGINE=InnoDB AUTO_INCREMENT=126 DEFAULT CHARSET=utf8;  
 
        要求使用存储过程“根据父分类代号(c_code)取得所有子分类及孙子分类”。
 
        使用以下存储过程:
        1. 主存储过程,作用是创建临时表,并操作其他存储过程或函数实现需求,其中临时表的作用是存储每个子分类的代号。流程:创建临时表——调用存储过程(category_findCodesByParentCode_queryAndInsert)取得所有子分类及孙子分类的代码并存入临时表中——调用函数(category_generateResult)生成结果字符串——删除临时表数据——返回生成的字符串。
[java]  
CREATE PROCEDURE category_findCodesByParentCode(in cCode varchar(200))  
begin  
-- 调用的函数或存储过程:category_findCodesByParentCode_queryAndInsert、category_generateResult  
-- 被调用于函数或存储过程:无  
    declare cRand varchar(50) default RAND();  
    declare result varchar(4000);  
  
    create temporary table if not exists tb_system_temp_category_categoryTree(  
        c_result varchar(4000),  
        c_rand varchar(50)  
    );    www.2cto.com  
  
    set max_sp_recursion_depth  = 100;  
  
    call category_findCodesByParentCode_queryAndInsert_zh(cCode, cRand);  
      
    set result = category_generateResult(cRand);  
  
    set @mySql = CONCAT('delete from tb_system_temp_category_categoryTree where c_rand = "',cRand,'"');  
    prepare stmt from @mySql;  
    execute stmt;  
  
    set @mySql = CONCAT('select "', result, '" from tb_system_user limit 0,1');  
    prepare stmt from @mySql;  
    execute stmt;  
end  
        2. 递归取得所有子分类及孙子分类并存储到临时表中。流程:根据父分类代号查询下级子分类代号,并通过指针迭代之——在迭代过程中,将子分类的代号存入临时表——调用函数(category_findChildrenCountByCode)检查子分类是否有下级分类,若无不管之;若有则递归调用存储过程(category_findCodesByParentCode_queryAndInsert)取得孙子分类。
[java]  
CREATE PROCEDURE category_findCodesByParentCode_queryAndInsert(in cCode varchar(200), in cRand varchar(50))  
begin  
-- 调用的函数或存储过程:category_findChildrenCountByCode、category_findCodesByParentCode_queryAndInsert  
-- 被调用于函数或存储过程:category_findCodesByParentCode  
    declare finished int default 0;  
    declare thisCode varchar(200);  
    declare cur cursor for select c_code from tb_system_category where c_parent_id in (select id from tb_system_category where c_code = cCode);  
    declare continue handler for not found set finished = 1;  
    open cur;  
    fetch cur into thisCode;  
    while finished = 0 do  
        set @mySql = CONCAT('insert into tb_system_temp_category_categoryTree(c_result,c_rand) values("',thisCode,'","',cRand,'")');  
        prepare stmt from @mySql;  
        execute stmt;    www.2cto.com  
  
        if category_findChildrenCountByCode(thisCode) > 0 then  
            call category_findCodesByParentCode_queryAndInsert(thisCode, cRand);  
        end if;  
  
        fetch cur into thisCode;  
    end while;  
    close cur;  
      
end  
        3. 根据分类代号取得子分类的个数。
[java]  
CREATE FUNCTION category_findChildrenCountByCode(cCode varchar(200)) RETURNS int(11)  
BEGIN  
-- 调用的函数或存储过程:无  
-- 被调用于函数或存储过程:category_findCodesByParentCode_queryAndInsert  
    declare finished int default 0;  
    declare count int;  
    declare cur cursor for select count(id) from tb_system_category where c_code like CONCAT(cCode,'%') and c_code != cCode;  
    declare continue handler for not found set finished = 1;  
    open cur;  
    fetch cur into count;  
    close cur;  
    if count is null then  
        return 0;  
    else  
        return count;  
    end if;  
END  
        4. 从临时表中查出结果并组合成字符串。
[java]  
CREATE FUNCTION category_generateResult(cRand varchar(50)) RETURNS varchar(4000) CHARSET utf8    www.2cto.com  
BEGIN  
-- 调用的函数或存储过程:无  
-- 被调用于函数或存储过程:category_findCodesByParentCode  
    declare finished int default 0;  
    declare result varchar(20000) default '';  
    declare thisResult varchar(200) default '';  
    declare cur cursor for select c_result from tb_system_temp_category_categoryTree where c_rand = cRand;  
    declare continue handler for not found set finished = 1;  
    open cur;  
    fetch cur into thisResult;  
    while finished = 0 do  
          
        set result = concat(result, thisResult, ',');  
      
        fetch cur into thisResult;  
    end while;  
    close cur;  
  
    if result is null then  
        return result;  
    else  
        if RIGHT(result,1) = ',' then  
            set result = SUBSTR(result, 1, CHAR_LENGTH(result) - 1);  
        end if;  
        return result;  
    end if;  
END  
 
        在MySQL中,不能够直接使用函数实现递归,在上例中,使用存储过程递归调用,将需要的值存储到临时表中,然后通过对临时表进行操作后取得结果。
 

看完上述内容,你们对MySQL中怎么实现存储过程递归调用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. mysql 存储过程实现搬表
  2. MySQL存储过程详解 mysql 存储过程

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql

上一篇:MYSQL中怎么管理二进制日志

下一篇:java如何实现卷积神经网络CupCnn

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》