Requires Free Membership to View
You sure can call a procedure from within that same procedure. Such an operation is called "recursion". One procedure recursively calls itself. The big thing to keep in mind is that you need an ending point otherwise the recursive calls will be infinite and never stop. A common recursive procedure or function is to compute the factorial of a number. N factorial (written N!) is defined as N*(N-1)*(N-2)*...*2*1. This also means that N! = N*(N-1)!. So a recursive function is born. Such a recursive function would appear as follows:
CREATE FUNCTION fact (n NUMBER)
RETURN NUMBER IS
returnVal NUMBER;
BEGIN
IF (n = 1) THEN
returnVal := 1;
ELSE
returnVal := n*fact(n-1);
END IF;
RETURN returnVal;
END;
/
Notice that in the above example, I have a terminating condition
(n=1).
If this condition is not met, then the function is called again. The
same concept applies to procedures. But ensure that you have a
terminating condition!
This was first published in August 2004
Join the conversationComment
Share
Comments
Results
Contribute to the conversation