At times I have been following questions that involve the term procedure
in database, then doubts:
procedure
? If possible use examples clear the mind of the layman (mine).
At times I have been following questions that involve the term procedure
in database, then doubts:
procedure
? If possible use examples clear the mind of the layman (mine).
Stored Procedure, translated Stored Procedure, is a library of SQL commands for use next to the database. It stores repetitive tasks and accepts input parameters so that the task is performed according to individual needs.
A Stored Procedure can reduce network traffic, improve database performance, create scheduled tasks, reduce risk, create processing routines, and more. For all these and other features, stored procedures are of the utmost importance to DBAs and developers.
Source: link
Imagine the following scenario, I have a table with the name, id, and salary fields. Every semester end the company provides a standard increase of 300 reais and a raise of 500 reais for all outstanding employees who receive positive remarks, you could elaborate a procedure to make this change in salaries, as below:
DELIMITER $$
CREATE PROCEDURE AumentarSalario(IN quantidadeObservacoesPositivas INT, codigoFuncionario INT)
BEGIN
IF quantidadeObservacoesPositivas = 0 THEN
<update que aumenta salario em 300 usando o codigoFuncionario recebido>
ELSE
<update que aumenta salario em 500 usando o codigoFuncionario recebido>
END IF
END $$
DELIMITER ;
So you could use it every time you run this account for an employee.
Procedure are sql command blocks, we use to concentrate codes, especially when we will use them more than once.