首页 > 数据库技术 > 详细

mysql存储过程递归调用

时间:2021-04-09 23:37:33      阅读:26      评论:0      收藏:0      [点我收藏+]
mysql中的存储过程是可以递归调用的,如果报错Recursive limit 0 请使用 set @@max_sp_recursion_depth = 100 修改递归深度。
drop procedure if exists pro;
drop table if exists data;

set @@max_sp_recursion_depth = 100;-- 设置递归深度
create table data(ans varchar(100));
create procedure pro(in n int)
begin
	if n <> 1 and n <> 145 then
		set @sum = 0, @s = ‘‘;
		while n <> 0 do
			set @a = n % 10, @sum = @sum + @a * @a, n = n div 10;
			set @s = concat(@s,@a*@a);
			if n <> 0 then
				set @s = concat(@s,‘+‘);
			end if;
		end while;
		set @s = concat(@s,‘=‘,@sum);
		insert into data values(@s);
		call pro(@sum);-- 递归调用
	end if;
end;

delimiter ;
call pro(12345);
select * from data;

  

mysql存储过程递归调用

原文:https://www.cnblogs.com/lemu/p/14637702.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!