In a recursive definition, something is defined in terms of simpler versions of itself. Consider the definition of n factorial (n!), the product of all integers from 1 to n:
n! = n * (n - 1)!
There must be at least two paths through a recursive subprogram: one that leads to the recursive call and one that does not. That is, at least one path must lead to a terminating condition. Otherwise, the recursion would (theoretically) go on forever. In practice, if a recursive subprogram strays into infinite regress, PL/SQL eventually runs out of memory and raises the predefined exception STORAGE_ERROR.
0! = 1 1! = 1 * 0! = 1 * 1 = 1 2! = 2 * 1! = 2 * 1 = 2 3! = 3 * 2! = 3 * 2 = 6
To implement this algorithm, you might write the following recursive function, which returns the factorial of a positive integer:
FUNCTION fac (n POSITIVE) RETURN INTEGER IS -- returns n!
BEGIN
IF n = 1 THEN -- terminating condition
RETURN 1;
ELSE
RETURN n * fac(n - 1); -- recursive call
END IF;
END fac;At each recursive call, n is decremented. Eventually, n becomes 1 and the recursion stops.
find_staff(7839);
The procedure passes mgr_no to a cursor in a cursor FOR loop, which finds staff members at successively lower tiers in the organization. At each recursive call, a new instance of the FOR loop is created and another cursor is opened, but prior cursors stay positioned on the next row in their result sets. When a fetch fails to return a row, the cursor is closed automatically and the FOR loop is exited. Since the recursive call is inside the FOR loop, the recursion stops.
PROCEDURE find_staff (mgr_no NUMBER, tier NUMBER := 1) IS
boss_name CHAR(10);
CURSOR c1 (boss_no NUMBER) IS
SELECT empno, ename FROM emp WHERE mgr = boss_no;
BEGIN
/* Get manager's name. */
SELECT ename INTO boss_name FROM emp WHERE empno = mgr_no;
IF tier = 1 THEN
INSERT INTO staff -- single-column output table
VALUES (boss_name || ' manages the staff');
END IF;
/* Find staff members who report directly to manager. */
FOR ee IN c1 (mgr_no) LOOP
INSERT INTO staff
VALUES (boss_name || ' manages ' || ee.ename
|| ' on tier ' || to_char(tier));
/* Drop to next tier in organization. */
find_staff(ee.empno, tier + 1); -- recursive call
END LOOP;
COMMIT;
END;Unlike the initial call, each recursive call passes a second actual parameter (the next tier) to the procedure.
The last example illustrates recursion, not the efficient use of set-oriented SQL statements. You might want to compare the performance of the recursive procedure to that of the following SQL statement, which does the same task:
INSERT INTO staff
SELECT PRIOR ename || ' manages ' || ename
|| ' on tier ' || to_char(LEVEL - 1)
FROM emp
START WITH empno = 7839
CONNECT BY PRIOR empno = mgr;The SQL statement is appreciably faster. However, the procedure is more flexible. For example, a multi-table query cannot contain the CONNECT BY clause. So, unlike the procedure, the SQL statement cannot be modified to do joins. (A join combines rows from two or more database tables.) In addition, a procedure can process data in ways that a single SQL statement cannot.
.)
FUNCTION odd (n NATURAL) RETURN BOOLEAN; -- forward declaration
FUNCTION even (n NATURAL) RETURN BOOLEAN IS
BEGIN
IF n = 0 THEN
RETURN TRUE;
ELSE
RETURN odd(n - 1); -- mutually recursive call
END IF;
END even;
FUNCTION odd (n NATURAL) RETURN BOOLEAN IS
BEGIN
IF n = 0 THEN
RETURN FALSE;
ELSE
RETURN even(n - 1); -- mutually recursive call
END IF;
END odd;When a positive integer n is passed to odd or even, the functions call each other by turns. At each call, n is decremented. Ultimately, n becomes zero and the final call returns TRUE or FALSE. For instance, passing the number 4 to odd results in this sequence of calls:
odd(4) even(3) odd(2) even(1) odd(0) -- returns FALSE
On the other hand, passing the number 4 to even results in the following sequence of calls:
even(4) odd(3) even(2) odd(1) even(0) -- returns TRUE
-- recursive version
FUNCTION fib (n POSITIVE) RETURN INTEGER IS
BEGIN
IF (n = 1) OR (n = 2) THEN
RETURN 1;
ELSE
RETURN fib(n - 1) + fib(n - 2);
END IF;
END fib;
-- iterative version
FUNCTION fib (n POSITIVE) RETURN INTEGER IS
pos1 INTEGER := 1;
pos2 INTEGER := 0;
cum INTEGER;
BEGIN
IF (n = 1) OR (n = 2) THEN
RETURN 1;
ELSE
cum := pos1 + pos2;
FOR i IN 3..n LOOP
pos2 := pos1;
pos1 := cum;
cum := pos1 + pos2;
END LOOP;
RETURN cum;
END IF;
END fib;The recursive version of fib is more elegant than the iterative version. However, the iterative version is more efficient; it runs faster and uses less storage. That is because each recursive call requires additional time and memory. As the number of recursive calls gets larger, so does the difference in efficiency. Still, if you expect the number of recursive calls to be small, you might choose the recursive version for its readability.