What is a database database?

Asked

Viewed 21,372 times

8

For some time I have been following issues involving the term procedure in database, then doubts:

  1. What is procedure?
  2. Where to use it, what’s the point?

If possible examples of use clear the mind of the layman(my).

  • If I’m not mistaken, you may have a different understanding between several Engins, diverging in detail. However, in general it implies a set of imperatively ordered commands; T-SQL even allows the use of variables, loops and conditionals in the same way as any other imperative language

2 answers

10


What is

Stored Procedure, translated Stored Procedure, is an SQL command library 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 the performance of a database, create scheduled tasks, decrease risk, create process routines, etc. For all these and other features is that stored procedures are of utmost importance to Dbas and developers.

When to use procedures

  • When we have several applications written in different languages, or run on different platforms, but perform the same function.
  • When we prioritize consistency and security.

Source: http://codigofonte.uol.com.br/artigos/tudo-sobre-procedures

Examples

Mysql

Imagine the following scenario, I have a table with the fields name, id and salary. Every end of semester the company provides a standard increase of 300 reais and an increase of 500 reais for all employees with highlights, who receive positive observations, you could draft a precedent to carry out this change in wages, 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 whenever you hold this account for an employee.

Summary

Procedure are blocks of sql commands, we use to concentrate code, especially when we use them more than once.

  • 4

    There is something that bothers me about your answer. Maybe lack of examples? Or mention of the distinction between several Engineering? Mention of side effects?

  • 1

    I did not put examples, because the question was theoretical, I saw no need, but you may be right. The second part I do not understand what bothers you explains me and we will try to improve.

  • 1

    I edited there asking for an example ...heuehuehu Vlq the attention man..

  • 1

    @Luizsantos yes, so +1, but still it bothered me. I don’t know, it just bothered me

  • 1

    So, it may have been a lack of affection my question, I might have understood better and have had the affection to put an example or quote more data, was incomplete. @Jeffersonquesado

  • 1

    @Jeffersonquesado and Magichat I put an example, see if it is clear and to understand well.

  • Mysql the example, right?

  • 1

    Right! vopu edit again, thanks for the help Jefferson

  • 1

    @Jeffersonquesado Thanks for the editing and help :)

  • @Luizsantos, needing we are here. I am neurotic with textual aesthetics

  • I came in here to help, so let’s go together we help and improve

  • Guys, I worked with stored procedures on Oracle a long time ago and I don’t know if this being "an SQL command library" would be very accurate. First, in Oracle we use the PL/SQL, which is a more extensive and procedural language than SQL (it seems to me a superconjointly), both to declare the SP and to call other Sps, which can be defined by the user or available in the database. Sps do not seem to be anything more than functions (receive arguments and return value) accessible in the context of the database. In Pascal they would be"functions" (which return value) and not "procedures".

  • I extrapolate that the concept is similar in other banks. They are persisted (hence the "stored"), i.e., they are saved in the bank when created or changed in order to be later called (and there is probably some caching invocations). So the pre-available ones in the database I think one could even call "SQL function library" but the ones defined by the user are also Sps available in the context of the database and this makes the definition a little wider than the current definition implies. I think we should at least mention that they are function, user-defined or...

  • ...not, stored in a bank and available for access in the context of the bank. So to summarize, as my experience with Sps is not very extensive, I’m making this suggestion here via comment for others to analyze. I wouldn’t use the term library, because by user-defined and the system-defined would already be at least two distinct for the same definition.

Show 9 more comments

0

According to Rodrigo Dornel:

One procedure is a block of commands or SQL statements organized to perform one or more tasks. It can be used to be triggered by a simple call that executes a series of other commands.

Example:

CREATE PROCEDURE uspRetornaIdade
@CodigoCliente int
AS
SELECT Clientes.ClienteNome, YEAR(GETDATE())-YEAR(ClienteNascimento) AS IDADE
FROM Clientes
INNER JOIN Contas ON Clientes.ClienteCodigo=Contas.ClienteCodigo
WHERE Clientes.ClienteCodigo = @CodigoCliente;

And this is a definition on the Microsoft website:

Stored procedures are similar to procedures in other programming languages in the sense that they can:

  • Accept input parameters and return multiple values in the output parameters form for call or batch procedure.
  • Contain programming instructions that perform operations in the database, including calling other procedures.
  • Return a status value to a call or batch procedure to indicate success or failure (and the reason for failure).
  • Welcome to [en.so], Mclaver89! It is not appropriate to copy articles as a response. We want original answers, what really counts is the answer you create. Every response can make references to other sources that help complement your response, but the site was not meant to do a Google search and copy what you find here. Read more about this on copies of articles. You can click [Edit] to improve your answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.