drop function if exists f1; create function f1(n int) returns int deterministic begin declare total int default 0; while n>0 do set total := total +n; set n := n - 1; end while; return total; end;
mysql> select f1(10); +--------+ | f1(10) | +--------+ | 55 | +--------+ 1 row in set (0.00 sec)