Differences between declare and set variables in MySQL stored procedures, mysqldeclare

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.