MySQL stored procedure Pass in, out, inout parameter usage

Source: Internet
Author: User

Stored ProceduresPass the reference: A parameter can be declared in parentheses in a stored procedure. The syntax is CREATE PROCEDURE P ([in/out/inout] Parameter Name argument type:) In: The parameter is passed to the value, the defined parameter gets the value out: The parameter of the schema definition can only be assigned within the procedure body, indicating that the parameter may pass a value callback with his procedure (inside the stored procedure,the initial value of this parameter is null, regardless of whether the caller sets a value for the stored procedure parameter) InOut:The caller can also pass values to the stored procedure via the InOut parameter, you can also transfer values from inside the stored procedure to the caller
If you just want to pass the data to the MySQL stored procedure, use the "in" type parameter, and if you only return the value from the MySQL stored procedure, use the "out" type parameter, and if you need to pass the data to the MySQL stored procedure, and then pass it back to us after some calculation, we use " InOut "type parameter. MySQL stored procedure Parameters If you do not explicitly specify "in", "Out", and "InOut", the default is "in".
instance one: Stored procedure parameter in
DELIMITER $ $CREATE PROCEDURE p1 (in num int) BEGIN  DECLARE i INT DEFAULT 0;  DECLARE Total INT DEFAULT 0;  While I<=num does    SET total: = i + total;    SET I: = i+1;  END while;  SELECT Total; end$$


Example two:stored procedure pass -through argument out
CREATE PROCEDURE p2 (out num INT) BEGIN SELECT num as num_1; IF (num is not NULL) then SET num = num + 1; SELECT num as num_2; ELSE SELECT 1 into Num; END IF; SELECT num as Num_3; end$ $SET @num = 10$ $CALL P2 (@num) $ $SELECT @num as num_out$$


Example Three:stored Procedure pass parameter InOut
CREATE PROCEDURE P3 (INOUT Age int.) BEGIN  SET Age: = age + 20; end$ $set @currage =18$ $call P3 (@currage) $ $select @currage $$


MySQL stored procedure Pass in, out, inout parameter usage

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.