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.

By ,

May 25, 2015

a

You might also like…

No-code: Ease or Trap? What no one tells you about creating without coding

With the popularization of No-code and Low-code platforms, application development has reached a po...

Top 10 Rapid App Development Tools You Need to Know

In this highly fast-moving digital world, rapid application development tools must be available to ...

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.