The variables are part of the routine for any programmer or DBA, since it is from them that many functions are triggered and make modifications within a system. In the case of databases, more specifically MySQL, variables assist in performing a variety of tasks.
By default, the variables are created in MySQL as follows:
@var1:= 1
After the indication of the special character “@”, one just need to indicate the value to be stored in the variable. In the example, the variable receives the integer value “1”.
If you need to set a new value to the variable, the SET command must be used as shown in the example below:
SET @var1 = 2
The variables in the database are more often used in procedures, and that is because they are, in most cases, indicated by the user who will work with the change.
As an example, let’s create a procedure that will perform a calculation from a received information shaped as a variable:
CREATE PROCEDURE ‘sp_RaiseSquare’ (INOUT val INT)
BEGIN
SET val = val * val;
END
Once created the procedure, let’s call it also using the variables feature:
SET @var1 = 2
CALL ElevarQuadrado(@var1);
SELECT @var1;
In this case, the printed value on the screen will be the number 4.
In a simple example, you can see how the variables are present in solving almost every problem. Therefore, understanding its operation helps solve everyday problems more quickly and effectively.
Check out more content on our blog!
Learn all about Scriptcase.
You might also like…