Inout Example for a Mysql Stored Procedure

Could you please give me a simple example of INOUT in MySQL stored procedure?

3 Answers

I think searching Google will give you tons of examples!!
One (taken from here)

DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`get_users` $$
CREATE PROCEDURE `get_users`(
    IN firstName VARCHAR(100),
    OUT totalUsers INT
)
BEGIN
    SELECT COUNT(*) INTO totalUsers
    FROM users
    WHERE first_name = firstName;
    SELECT * FROM users
    WHERE first_name = firstName;
END $$
DELIMITER ;

Post says:

Notice there are two statements in the body of this stored procedure. The first select count(*) statement counts the total number of people who’s first name is equal to the in variable firstName. Once it’s gets the count, it sets the out variable totalUsers to that value.

The next statement is a simple select. This will select all fields for users who’s first name is equal to the in variable firstName and return the recordset. So by calling this stored procedure and passing in two parameters (first name, total), a recordset will be returned and an out variable will be set – that can then be queried.

EDITED:
As in MySQL website:

CREATE PROCEDURE p (OUT ver_param VARCHAR(25), INOUT incr_param INT)
BEGIN
  # Set value of OUT parameter
  SELECT VERSION() INTO ver_param;
  # Increment value of INOUT parameter
  SET incr_param = incr_param + 1;
END;
3

Example of INOUT usage in MySQL:

From the terminal run mysql -u root -p.

el@apollo:~$ mysql -u root -p
Enter password: 

Change to your database:

mysql> use yourdb;
Reading table information for completion of table and column names

Create a variable to feed into your stored procedure called msg.

mysql> select 'ricksays' into @msg;
Query OK, 1 row affected (0.00 sec)

Create the stored procedure:

mysql> delimiter //
mysql> create procedure foobar (inout msg varchar(100))
-> begin
-> set msg = concat(@msg, " never gonna let you down");
-> end//

Set the delimiter back:

mysql> delimiter ;

Invoke the stored procedure, pass in the variable.

mysql> call foobar(@msg);
Query OK, 0 rows affected (0.00 sec)

Ok, Now see if it worked:

mysql> select @msg;
+-----------------------------------+
| @msg                              |
+-----------------------------------+
| ricksays never gonna let you down |
+-----------------------------------+
1 row in set (0.00 sec)

Ok it worked, it joined the strings together. So you defined a variable msg, passed in that variable into stored procedure called foobar, and @msg was written to by foobar.

Which is the purpose of the INOUT parameter. You could use this to solve problems.

If like me, the confusion was the difference between OUT and INOUT. Which was clarified with these points found in the MySQL Certification Study Guide

OUT - Any value the parameter has when it is passed is ignored by the procedure, and its initial value within the procedure is NULL

INOUT - The value passed by the caller is the parameter's initial value within the procedure

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article