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$$