Mysql, doubt about variables

3

Mysql allows you to use various types of syntax in the declaration of variables eg:

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

Do you have any advantage in using one type or another, or some disadvantage or even some recommendation of when to use one or the other?

    
asked by anonymous 12.06.2015 / 21:07

2 answers

5

Variables with @ are user-defined variables , and have session scope. That is, it is valid from the moment the connection to the server is opened, until the connection is closed.

The variables you create with DECLARE are local variables of a function or stored procedure , and are recreated with every call of the function or procedure where they are declared.

    
12.06.2015 / 23:02
-1
This question is very personal, it is the same as asking a developer what advantage to use one or another programming language, he says that "It depends on what you are going to do, how you are going to do it and why you will do it" and a series of other advantages and disadvantages.

It is known that certain types of statements allocate more memory and some less, but even those that allocate less memory if mistakenly consumed during the execution of the software will crash with your server.

I advise you to read this case and understand it better than I am speaking.

Using Variables in MySQL

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

    
12.06.2015 / 22:35