Mysql递归存储过程...达到限制0 ...无法更改max_sp_recursion_depth变量

时间:2010-10-29 22:41:03

标签: sql mysql stored-procedures

更新:好的,我能够调试这个并发现通过执行此操作select @@max_sp_recursion_depth我可以看到变量设置为15,但是,当我用{{1运行回调自身时我得到的错误表明已达到递归限制0,但它从未迭代过一次'


当我运行下面的递归存储过程时,我继续得到“超出递归限制0”,即使我将其设置为CALL single_limb_portfolio_list(xaccount_id, xparent_portfolio_id);

我是否将此变量设置在错误的位置?它的表现就像永远不会被设置并始终保持为零?

提前感谢您的帮助。

递归存储过程:

SET max_sp_recursion_depth = 15 ;

1 个答案:

答案 0 :(得分:0)

这是一个非递归的解决方案,你可以适应你的模型(http://pastie.org/1259300)

drop table if exists product;

create table product
(
prod_id smallint unsigned not null auto_increment primary key,
name varchar(255) not null,
parent_id smallint unsigned null,
key (parent_id)
)engine = innodb;


insert into product (name, parent_id) values
('Products',null), 
   ('Systems & Bundles',1), 
   ('Components',1), 
      ('Processors',3), 
      ('Motherboards',3), 
        ('AMD',5), 
        ('Intel',5), 
           ('Intel LGA1366',7);


delimiter ;

drop procedure if exists product_hier;

delimiter #

create procedure product_hier
(
in p_prod_id smallint unsigned
)
begin

declare v_done tinyint unsigned default 0;
declare v_depth smallint unsigned default 0;

create temporary table hier(
 parent_id smallint unsigned, 
 prod_id smallint unsigned, 
 depth smallint unsigned default 0
)engine = memory;

insert into hier select parent_id, prod_id, v_depth from product where prod_id = p_prod_id;

/* http://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html */

create temporary table tmp engine=memory select * from hier;

while not v_done do

    if exists( select 1 from product p inner join hier on p.parent_id = hier.prod_id and hier.depth = v_depth) then

        insert into hier 
            select p.parent_id, p.prod_id,  v_depth + 1 from product p 
            inner join tmp on p.parent_id = tmp.prod_id and tmp.depth = v_depth;

        set v_depth = v_depth + 1;          

        truncate table tmp;
        insert into tmp select * from hier where depth = v_depth;

    else
        set v_done = 1;
    end if;

end while;

select 
 p.prod_id,
 p.name as prod_name,
 b.prod_id as parent_prod_id,
 b.name as parent_prod_name,
 hier.depth
from 
 hier
inner join product p on hier.prod_id = p.prod_id
inner join product b on hier.parent_id = b.prod_id
order by
 hier.depth, hier.prod_id;

drop temporary table if exists hier;
drop temporary table if exists tmp;

end #

delimiter ;


call product_hier(3);

call product_hier(5);