MySQL stored procedure Pass in, out, inout parameter usage

Source: Internet
Author: User

Stored Proceduresparameter: In the parentheses of the stored procedure, you can declare the parameters. 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 parameterInOutcallers can also pass values to the stored procedure through the InOut parameter, or they can pass 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.
  1. DELIMITER $$
  2. CREATE PROCEDURE p1 (innum INT)
  3. BEGIN
  4. DECLARE i INT DEFAULT 0;
  5. DECLARE Total INT DEFAULT 0;
  6. While I<=num do
  7. SET Total: = i + all;
  8. SET I: = i+1;
  9. END while;
  10. SELECT Total;
  11. end$$


Stored procedure pass-through argument out [SQL]View Plain copy
  1. CREATE PROCEDURE p2 (outnum INT)
  2. BEGIN
  3. SELECT num as num_1;
  4. IF (num is isn't NULL) Then
  5. SET num = num + 1;
  6. SELECT num as num_2;
  7. ELSE
  8. SELECT 1 into num;
  9. END IF;
  10. SELECT num as num_3;
  11. end$$
  12. SET @num = 10$$
  13. Call P2 (@num) $$
  14. SELECT @num as num_out$$


    1. CREATE PROCEDURE P3 (INOUT age INT)
    2. BEGIN
    3. SET Age: = age + 20;
    4. end$$
    5. Set @currage =18$$
    6. Call P3 (@currage) $$
    7. 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.