Differences between declare and set-defined variables in MySQL stored procedures

Source: Internet
Author: User

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

Related Article

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.