Usage of input, out, And inout parameters in MySQL stored procedures

Source: Internet
Author: User

Usage of input, out, And inout parameters in MySQL stored procedures
Stored procedure parameters: You can declare parameters in the brackets of the stored procedure. Syntax: create procedure p ([in/out/inout] parameter name parameter type ..) in: input a value to the parameter, and the defined parameter gets the value out: the parameter defined in the mode can only be assigned within the process body, this parameter can pass a value back to the calling process (in 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 the value to the stored procedure through the inout parameter, or pass the value to the caller from the stored procedure.
If you only want to pass data to the MySQL stored procedure, use the "in" type parameter. If you only return values from the MySQL stored procedure, use the "out" type parameter; if you need to pass the data to the MySQL stored procedure, you need to pass it back to us after some computation. In this case, you need to use the "inout" type parameter. If you do not explicitly specify "in", "out", and "inout" For MySQL stored procedure parameters, the default value is "in ".
Example 1: stored procedure parameters in

DELIMITER $$CREATE PROCEDURE p1(IN num INT)BEGIN  DECLARE i INT DEFAULT 0;  DECLARE total INT DEFAULT 0;  WHILE i<=num DO    SET total := i + total;    SET i := i+1;  END WHILE;  SELECT total;END$$


Example 2: stored procedure parameter passing 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 3: Stored Procedure Parameter inout
CREATE PROCEDURE p3(INOUT age INT)BEGIN  SET age := age + 20;END$$set @currage =18$$call p3(@currage)$$select @currage$$


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.