DELIMITER $$ CREATE DEFINER=`root`@`localhost` FUNCTION `doble`(numero int) RETURNS int(11) BEGIN RETURN numero*2; END$$ DELIMITER ; DELIMITER $$ CREATE DEFINER=`root`@`localhost` FUNCTION `tipo_pelicula` (p_film_id int) RETURNS varchar(50) CHARSET latin1 BEGIN declare v_length int; select length into v_length from film where film_id=p_film_id; if v_length<=140 then return 'CORTA'; else RETURN 'LARGA'; end if; END$$ DELIMITER ; DELIMITER $$ USE `sakila`$$ CREATE DEFINER=`root`@`localhost` FUNCTION `factorial`(p_numero int) RETURNS int(11) BEGIN declare total,cont int default 1; while cont<=p_numero do set total=total*cont; set cont=cont+1; end while; RETURN total; END$$ DELIMITER ;
Uso:
select doble(8); select title,length,tipo_pelicula(film_id) from film; select factorial(5);