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