存储过程实现递归算法

目录

1. Oracle

2. MySQL


        以经典的阶乘算法为例。

1. Oracle

create or replace procedure factorial(n in number, b out number)  
is  
    c_in number;  
    c_out number;  
begin  
    if n!=1 then  
        c_in := n-1;  
        factorial(c_in,c_out);  
        b := n*c_out;  
    else  
        b := 1;  
    end if;  
end;  
/  
  
--调用  
variable r number;  
exec factorial(6,:r);  
print r;  

2. MySQL

drop procedure if exists factorial;  
set @@session.max_sp_recursion_depth=99;  
delimiter $$  
  
create procedure factorial (in n bigint, out b bigint)  
begin  
    declare c_in bigint;  
    declare c_out bigint;  
    if n!=1 then  
        set c_in := n-1;  
        call factorial(c_in,c_out);  
        set b:=n*c_out;  
    else  
        set b:=1;  
    end if;  
end$$  
  
delimiter ;  
  
--调用  
call factorial(6,@r);  
select @r;  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值