Variables defined by declare and variables defined by @set are often seen in stored procedures. In simple terms, the declare definition is similar to a local variable, @set defined similar to a global variable.
1. The variable defined by declare is similar to a local variable in a Java class and only takes effect in the class. That only takes effect between begin and end in the stored procedure.
2, @set defined variables, called session variables, also known as user-defined variables, throughout the session play a role (such as an application of a connection process), that is, the variable can be called to share data between the stored procedure or code. How to understand it? You can see the following simple example, well understood.
(1) First execute the following script, create a stored procedure, respectively, declare form of variables and @set form of variables
DROP PROCEDURE IF EXISTS Temp;D Elimiter//CREATE PROCEDURE Temp()BEGIN DECLAREAINT DEFAULT 1; SETA=A+1; SET @b=@b+1; SELECTA@b;END//DELIMITER;
(2) Then initialize the B variable.
SET @b = 1
(3) Then call this stored procedure repeatedly.
Temp ();
(4) You will find that the value of a does not change, and the value of B will always increase.
So, summing up is the beginning of the sentence, declare defined similar to a local variable, @set defined similar global variables.
The difference between declare and set definition variables in a MySQL stored procedure