Mysql, doubt about variables

Asked

Viewed 577 times

3

Mysql allows using various type of syntax in variable declaration ex:

declare i int unsigned default 0;
set @str = "select ";

Has some advantage in using one type or another, or some disadvantage or even some recommendation of when to use one or the other?

2 answers

5


The variables with @ sane user-defined variables, and have session scope. That is, it is valid from the moment the connection with the server is opened, until the closure of that connection.

The variables you create with DECLARE are local variables of a function or stored Procedure, and are recreated at each function call or Procedure where they are declared.

  • I used indiscriminately, I already made stored Procedure with both and equal wheel, but then in short it means that user-defined I can use them within a Function or sp and continue with the value of it in another function call or sp if I keep connection ?

  • 1

    @Sneepsninja that’s right

-1

This question is very personal, it is the same as asking a developer what advantage to use one or another programming language, he will say that "It depends on what you will do, how you will do and why you will do it" and a number of other advantages and disadvantages.

it is known that certain types of statements allocate more memory and others not so much, but even those that allocate less memory if consumed incorrectly while running the software will crash with your server.

I advise you to read this case and understand better than I’m talking about.

Using variables in Mysql

I hope I have helped you (the best syntax is the one that best suits your purposes).

  • 1

    It is not personal in this case, there is a difference between the variables created with the two syntaxes he indicated in the question.

Browser other questions tagged

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