The in, out, and inout parameters in the MySQL stored procedure use the actual case

Source: Internet
Author: User

1. Use of parameter in (on behalf of input, meaning that your parameters will be passed to the process of storage)
To avoid a semicolon (";") in the stored procedure In the concluding sentence, we use the delimiter to tell the MySQL interpreter whether the command is over.
/**
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) $

After you create and execute the stored procedure, the results are as follows:

2. Use of parameter out (on behalf of outgoing output)
One thing to note here is that our output parameters must be set to the corresponding type of initial, otherwise no matter how you calculate the results are null values
/**
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$
Note: We can understand the first input parameter, but how exactly should we lose the second output parameter?
Here we need to define a variable name for the second parameter (a more vivid point is that you enter the parameter n of an input type, the output parameter of total outward emitting output we just need to define a variable name to receive this output value)
Call P2 (@sum) $//The @sum here is the value I've defined to receive total everywhere.
Select @sum $
Create and execute the stored procedure (query defined variable values) and run the result as follows:

Summarize in, out differences:
In: Indicates input a value, you need a value, I give you a value
Out: You output a value, the value you output I will take a variable to receive the value you gave me the output

3. Use of parameter InOut (can enter a value and can pass a value)
/**
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.
Set @currentAge =8$
Call P3 (@currentAge) $
Select @currentAge $
After you create and execute the stored procedure, the results are as follows:

The in, out, and inout parameters in the MySQL stored procedure use the actual case

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.