Differences between declare and set variables in MySQL stored procedures, mysqldeclare
The variables defined by declare and those defined by @ set are often seen in the stored procedure. In short, declare defines local variables and @ set defines global variables.
1. the variables defined by declare are similar to local variables in the java class and take effect only in the class. That is, it takes effect only between begin and end in the stored procedure.
2. @ set variables are called session variables and User-Defined variables. They work throughout the session (for example, in a connection process of an application ), that is, this variable can share data between called stored procedures or codes. How can this problem be understood? Let's take a look at the simple example below, which is easy to understand.
(1) execute the following script to create a stored procedure with variables in the form of declare and variables in the form of @ set.
Drop procedure if exists temp; DELIMITER // create procedure temp () begin declare a int default 1; SET a = a + 1; SET @ B = @ B + 1; SELECT, @ B; END // DELIMITER;
(2) initialize the B variable.
SET @ B = 1;
(3) Call the stored procedure again.
CALL temp ();
(4) The value of a does not change, and the value of B increases continuously.
In summary, declare defines local variables and @ set defines global variables.
The difference between the declare and set variables in the MySQL stored procedure is that all the content shared by the editor. I hope you can provide a reference and support for the customer's house.