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));
delimiter ;;
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;