MySQL stored procedure in, out, inout parameters use

Source: Internet
Author: User

Inch

represent input, meaning that your parameters will be passed through the process. The basic can be understood as the parameter passed in the function, and if the argument is a variable, then the entire procedure process ends without affecting the external variable value.

/
Case function: Seeking 1-n and
Developer: Xu Shouwei
Date: 2016-08-13
/
Delimiter $
CREATE PROCEDURE p1 (in n int)
Begin
DECLARE total int default 0;
declare num int default 0;
While Num < n do
Set num:=num+1;
Set Total:=total+num;
End while;
Select Total;
end$**

Call P1 (10) $

Out

An external variable that can be understood as a function to be changed, regardless of what value the variable is outside, the initial value inside is null, and the internal effect on it changes the value of the external variable.

It is meaningless to pass an out parameter directly to a procedure, because the out value is initially null by default in procedure.

/
Case function: Seeking 1-n and
Developer: Xu Shouwei
Date: 2016-08-13
*/
CREATE PROCEDURE P2 (in n int,out total int)
Begin
declare num int default 0;
Set total:=0;
While Num < n do
Set num:=num+1;
Set Total:=total+num;
End while;
end$**

Call P2 (@sum) $//Here's the @sum I've defined to receive the value of total everywhere.
Select @sum $

InOut

You can enter both a value and a value. It can be understood that a function internally declares global and may modify the value of the variable.

/
Function: Pass an age, automatically let the age increase 10 years old
Developer: Xu Shouwei
Date: 2016-08-13
/
CREATE PROCEDURE p3 (inout age int)
Begin
Set age:=age+10;
end$
Note: When calling, I need to declare with you that the parameter value of the InOut type is both the input type and the output type, you give it a value, the value is not a variable, it is not the variable when it is out how to assign this value is not?
So we need to set a variable and initialize the value, and pass the variable directly when it is called. *

Call:
Set @currentAge =8$
Call P3 (@currentAge) $
Select @currentAge $**

MySQL stored procedure in, out, inout parameters use

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.