Variables defined by declare and @set are often seen in stored procedures. In simple terms, the declare definition is similar to a local variable, @set a similar global variable defined.
1. Declare-defined variables are similar to local variables in Java classes and only take effect in a class. That is, it only takes effect between begin and end in a stored procedure.
2, @set-defined variables, called session variables, also called user-defined variables that work throughout the session (such as during a connection to an application), that is, the variable can share data between the stored procedure or code being invoked. How do you understand it? Look at the following simple example, very well understood.
(1) Execute the following script first, create a stored procedure, have declare form of variable and @set form of variable
DROP PROCEDURE IF EXISTS temp;
DELIMITER//
CREATE PROCEDURE temp ()
BEGIN
DECLARE a INT DEFAULT 1;
SET a=a+1;
SET @b=@b+1;
SELECT a,@b;
End
//
DELIMITER;
(2) Then initialize the B variable.
SET @b=1;
(3) and then repeatedly call the stored procedure.
Call Temp ();
(4) The value of a will be found unchanged, while the value of B will continue to increase.
So, summed up is the beginning of the sentence, declare definition is similar to local variables, @set defined similar to global variables.
This article on the MySQL stored procedures in the declare and set definition variables are small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.