Initial situation:
When a function or procedure calls other ones, the creation order must be ascending, from the most sheltered function or procedure to which makes first call. But in recursivity case (routine which calls itself) it enters conflict, due to the referenced routine (which must exist), is the same one that makes the call.
Example of recursive function:
CREATE FUNCTION fibonacci (number integer)
RETURNS integer
NO EXTERNAL ACTION NOT DETERMINISTIC READS SQL DATA
BEGIN ATOMIC
IF number = 0 OR number = 1 THEN
RETURN 1;
ELSE
RETURN fibonacci(number - 1) + fibonacci(number - 2);
END IF;
END;
SQLSTATE 42884
Solution:
Using dynamic call (with EXECUTE IMMEDIATE clause), function would be something like this:
[...]
SET m_call = 'CALL fibonacci (' || number || ')';
EXECUTE IMMEDIATE m_call;
[...]
Where m_call is a variable defined like char...
Question:
What about the return value? How would be exactly fibonnacci function with dynamic call?