Recursion : Introduction « Procedure Function « MySQL Tutorial






mysql>
Functions and procedures can call themselves.
mysql>
mysql> delimiter $$
mysql> CREATE FUNCTION factorial(n BIGINT) RETURNS BIGINT
    -> BEGIN
    ->   IF n>=2 THEN
    ->     RETURN n * factorial(n-1);
    ->   ELSE
    ->     RETURN n;
    ->   END IF;
    -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql>
mysql>
mysql> SELECT factorial(3);
ERROR 1424 (HY000): Recursive stored functions and triggers are not allowed.
mysql>
mysql> drop function factorial;
Query OK, 0 rows affected (0.00 sec)








11.1.Introduction
11.1.1.Using user-defined function in a select statement
11.1.2.Use user-defined function in a select statement to deal with data in a table
11.1.3.Use user-defined function in order by clause
11.1.4.Use user-defined function in where clause
11.1.5.Recursion