What is a Stored Function, what is its basic syntax? How and where to use a Stored Function
What is a Stored Function, what is its basic syntax? How and where to use a Stored Function
Basic syntax of a Function in MySQL:
delimiter $
CREATE FUNCTION nome_da_funcao (parametros)
RETURNS INT -- pode retornar qualquer tipo de dado
BEGIN
-- codigo
return valor;
END$
delimiter;
The function works to make changes or methods that would save the programming job.
Ex. Discounting a certain product, Calculating tax, etc.
To call the function you can either make a simple call:
select funcao();
You can also assign the return value for a variable in MySQL:
declare valor;
set valor = funcao();
There are also Stored Procedures, which basically do not return value, but are called by a different command:
call procedure();
More information: