Subject | SPs and recursion |
---|---|
Author | duilio_fos <irel_llc@libero.it> |
Post date | 2003-02-20T20:35:34Z |
this is a recursive SP I found in the net:
CREATE PROCEDURE FACTORIAL (num INT)
RETURNS (n_factorial DOUBLE PRECISION)
AS
DECLARE VARIABLE num_less_one INT;
BEGIN
IF (num = 1) THEN
BEGIN
n_factorial = 1;
SUSPEND;
END
ELSE
BEGIN
num_less_one = :num - 1;
EXECUTE PROCEDURE FACTORIAL :num_less_one
RETURNING_VALUES :n_factorial;
n_factorial = :n_factorial * :num;
SUSPEND;
END
END
If I write
select * from factorial(5)
I get
120
which is the correct result allright.
In order to understand better how SPs work, I tried to modify the
code, so to get
5
20
60
120
i.e. to have the result returned at every call.
I could not find a way to do it.
Can you ?
TIA
Duilio Foschi
PS temporary tables make a too easy solution...
CREATE PROCEDURE FACTORIAL (num INT)
RETURNS (n_factorial DOUBLE PRECISION)
AS
DECLARE VARIABLE num_less_one INT;
BEGIN
IF (num = 1) THEN
BEGIN
n_factorial = 1;
SUSPEND;
END
ELSE
BEGIN
num_less_one = :num - 1;
EXECUTE PROCEDURE FACTORIAL :num_less_one
RETURNING_VALUES :n_factorial;
n_factorial = :n_factorial * :num;
SUSPEND;
END
END
If I write
select * from factorial(5)
I get
120
which is the correct result allright.
In order to understand better how SPs work, I tried to modify the
code, so to get
5
20
60
120
i.e. to have the result returned at every call.
I could not find a way to do it.
Can you ?
TIA
Duilio Foschi
PS temporary tables make a too easy solution...