What is the difference between a variable and a @variable variable in MySQL?

If we declare a variable in mysql as:

declare prev_year int;

Are set prev_year = 100;and set @prev_year = 100;the same?

What is the difference as they seem to act as different variables?

+5
source share
1 answer

No, they are not really the same ... but they can be considered the same depending on the context used.

I will explain. MySQL has session variables, these are variables that you assign that live until the end of the session (think of a connection variable). These variables are declared as a symbol @. This way you can execute these two statements and it will work:

SET @myId := 123;

SELECT * FROM table
WHERE id = @myId;

DECLARE, ,

DECLARE myId INT;

SELECT id INTO myId FROM table
WHERE name = 'steve';
+3

All Articles