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.

variaveis_msql

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.

You might also like…

Do they pay for your system or code?

For all those who develop system and softwares every day at work: Have you ever wondered what makes...

What is Business Intelligence anyways?

Surely you have listened about the expression, “BI” or “Business Intelligence”, the questio...

Scriptcase & Web Services

This time I will explain how to work with web services inside the Scriptcase, directly with PHP and...

You might also like…

Get new posts, resources, offers and more each week.

We will use the information you provide to update you about our Newsletter and Special Offers. You can unsubscribe any time you want by clinck in a link in the footer of any email you receive from us, or by contacting us at sales@scriptcase.net. Learn more about our Privacy Police.