Discussion on the difference between declare and set definition variables in Mysql stored procedure _mysql

Source: Internet
Author: User

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.

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.