MOCKSTACKS
EN
Questions And Answers

More Tutorials









MYSQL Stored routines procedures and functions

Stored procedure with IN, OUT, INOUT parameters


DELIMITER $$
DROP PROCEDURE IF EXISTS sp_nested_loop$$
CREATE PROCEDURE sp_nested_loop(IN i INT, IN j INT, OUT x INT, OUT y INT, INOUT z INT)
BEGIN
 DECLARE a INTEGER DEFAULT 0;
 DECLARE b INTEGER DEFAULT 0;
 DECLARE c INTEGER DEFAULT 0;
 WHILE a < i DO
 WHILE b < j DO
 SET c = c + 1;
 SET b = b + 1;
 END WHILE;
 SET a = a + 1;
 SET b = 0;
 END WHILE;
 SET x = a, y = c;
 SET z = x + y + z;
END $$
DELIMITER ;

Invokes (CALL) the stored procedure:

SET @z = 30;
call sp_nested_loop(10, 20, @x, @y, @z);
SELECT @x, @y, @z;

Result:

+------+------+------+
| @x | @y | @z |
+------+------+------+
| 10 | 200 | 240 |
+------+------+------+

An IN parameter passes a value into a procedure. The procedure might modify the value, but the modification is not visible to the caller when the procedure returns.

An OUT parameter passes a value from the procedure back to the caller. Its initial value is NULL within the procedure, and its value is visible to the caller when the procedure returns.

An INOUT parameter is initialized by the caller, can be modified by the procedure, and any change made by the procedure is visible to the caller when the procedure returns.

Conclusion

In this page (written and validated by ) you learned about MYSQL Stored routines procedures and functions . What's Next? If you are interested in completing MYSQL tutorial, your next topic will be learning about: MYSQL Indexes and Keys.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.