Questions about Stored Function

4

What is a Stored Function, what is its basic syntax? How and where to use a Stored Function

    
asked by anonymous 24.09.2017 / 00:54

1 answer

2

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:

  

Creating Stored Procedures MySQL
MySQL Stored Functions

    
24.09.2017 / 01:09