DELIMITER //
create function FactorialR (n int) returns int
begin
declare resultado int default 1;
set resultado=FactorialR(n-1)*n;
return resultado;
end//
DELIMITER ;
Asked
Active
Viewed 80 times
0

nbk
- 45,398
- 8
- 30
- 47

Zidane Ortiz Diaz
- 13
- 1
2 Answers
1
You are not to use recursion, but you can always use simple loops
Of course you can use Pocedures which allow recursive calls
create function FactorialR (n int) returns int begin declare resultado BIGint default 1; DECLARE m int DEFAuLT 1; loop_label: LOOP IF m = n THEN LEAVE loop_label; END IF; SET m = m + 1; set resultado=resultado*m; END LOOP; return resultado; end
SELECT FactorialR(4)
| FactorialR(4) | | ------------: | | 24 |
db<>fiddle here

nbk
- 45,398
- 8
- 30
- 47
0
MySQL functions are not allowed to be recursive. Recursive stored functions in MySQL

Tim Roberts
- 48,973
- 4
- 21
- 30