Detailed example of MySQL data storage process parameters

Detailed example of MySQL data storage process parameters

There are three types of MySQL stored procedure parameters: in, out, and inout. What are their respective functions and characteristics?

1. MySQL stored procedure parameters (in)

MySQL stored procedure "in" parameter: Similar to the value passing of function parameters in C language, this parameter may be modified inside the MySQL stored procedure, but the modification of the in type parameter is not visible to the caller.

drop procedure if exists pr_param_in;
create procedure pr_param_in
(
  in id int -- in type MySQL stored procedure parameter)
begin
  if (id is not null) then
   set id = id + 1;
  end if;
  select id as id_inner;
end;
set @id = 10;
call pr_param_in(@id);
select @id as id_out;
mysql> call pr_param_in(@id);
+----------+
| id_inner |
+----------+
| 11 |
+----------+

mysql> select @id as id_out;
+--------+
| id_out |
+--------+
| 10 |
+--------+

It can be seen that the value passed into the user variable @id is 10. After executing the stored procedure, the value inside the procedure is: 11 (id_inner), but the value of the external variable is still: 10 (id_out).

2. MySQL stored procedure parameters (out)

MySQL stored procedure "out" parameter: pass values ​​from within the stored procedure to the caller. Inside a stored procedure, the initial value of the parameter is null, regardless of whether the caller sets a value for the stored procedure parameter.

drop procedure if exists pr_param_out;
create procedure pr_param_out
(
  out id int
)
begin
  select id as id_inner_1; -- The initial value of id is null
  if (id is not null) then
   set id = id + 1;
   select id as id_inner_2;
  else
   select 1 into id;
  end if;
  select id as id_inner_3;
end;
set @id = 10;
call pr_param_out(@id);
select @id as id_out;
mysql> set @id = 10;
mysql>
mysql> call pr_param_out(@id);
+------------+
| id_inner_1 |
+------------+
| NULL |
+------------+

+------------+
| id_inner_3 |
+------------+
| 1 |
+------------+

mysql> select @id as id_out;
+--------+
| id_out |
+--------+
| 1 |
+--------+

It can be seen that although we set the user-defined variable @id to 10, after passing @id to the stored procedure, the initial value of id inside the stored procedure is always null (id_inner_1). Finally the id value (id_out = 1) is passed back to the caller.

3. MySQL stored procedure parameters (inout)

MySQL stored procedure inout parameters are similar to out parameters, and both can pass values ​​from within the stored procedure to the caller. The difference is that the caller can also pass values ​​to the stored procedure through inout parameters.

drop procedure if exists pr_param_inout;
create procedure pr_param_inout
(
  inout id int
)
begin
  select id as id_inner_1; -- The id value is the value passed by the caller if (id is not null) then
   set id = id + 1;
   select id as id_inner_2;
  else
   select 1 into id;
  end if;
  select id as id_inner_3;
end;
set @id = 10;
call pr_param_inout(@id);
select @id as id_out;
mysql> set @id = 10;

mysql>
mysql> call pr_param_inout(@id);
+------------+
| id_inner_1 |
+------------+
| 10 |
+------------+

+------------+
| id_inner_2 |
+------------+
| 11 |
+------------+

+------------+
| id_inner_3 |
+------------+
| 11 |
+------------+
mysql>
mysql> select @id as id_out;
+--------+
| id_out |
+--------+
| 11 |
+--------+

From the results, we can see that after we pass @id (10) to the stored procedure, the stored procedure finally returns the calculated value 11 (id_inner_3) to the caller. The behavior of inout parameters in MySQL stored procedures is similar to passing by reference in C language functions.

Through the above examples: if you just want to pass data to the MySQL stored procedure, then use the "in" type parameter; if you just return the value from the MySQL stored procedure, then use the "out" type parameter; if you need to pass the data to the MySQL stored procedure and then pass it back to us after some calculations, then use the "inout" type parameter.

Summarize

The above is all the content of this article about the detailed explanation of MySQL data storage process parameter examples. I hope it will help you understand MySQL. Interested friends can continue to refer to this site: Analysis of the selection problem of storing time and date types in MySQL, MySQL declaration of variables and stored procedure analysis, etc. If you have any questions, you can leave a message at any time and the editor will reply to you in time. Thank you friends for supporting this site!

You may also be interested in:
  • Two ways of storing scrapy data in MySQL database (synchronous and asynchronous)
  • Python example of storing JSON-like data in MySQL
  • How to install MySQL 5.7 on Ubuntu and configure the data storage path
  • How to change the database data storage directory in MySQL
  • How to move mysql5.7.19 data storage location in Centos7
  • How to implement Mysql switching data storage directory
  • Solution to changing the data storage location of the database in MySQL 5.7
  • MySQL database architecture details
  • MySQL spatial data storage and functions

<<:  VMware Workstation 14 Pro installation and activation graphic tutorial

>>:  Detailed explanation of the error when using Element-ui NavMenu submenu to generate recursively

Recommend

Linux checkup, understand your Linux status (network IO, disk, CPU, memory)

Table of contents 1. Core commands 2. Common comm...

Two ways to prohibit clearing the input text input cache in html

Most browsers will cache input values ​​by defaul...

A brief analysis of the difference between ref and toRef in Vue3

1. ref is copied, the view will be updated If you...

JavaScript event capture bubbling and capture details

Table of contents 1. Event Flow 1. Concept 2. DOM...

A brief analysis of mysql index

A database index is a data structure whose purpos...

Introduction to install method in Vue

Table of contents 1. Globally registered componen...

A detailed tutorial on using Docker to build a complete development environment

Introduction to DNMP DNMP (Docker + Nginx + MySQL...

How to deploy Redis 6.x cluster through Docker

System environment: Redis version: 6.0.8 Docker v...

MySQL uses init-connect to increase the implementation of access audit function

The mysql connection must first be initialized th...

JavaScript function detailed introduction

Any number of statements can be encapsulated thro...

Vue el-date-picker dynamic limit time range case detailed explanation

There are two situations 1. Start time and end ti...

How to import Tomcat source code into idea

Table of contents 1. Download the tomcat code 2. ...

Axios cancel request and avoid duplicate requests

Table of contents origin status quo Cancel reques...

Use of Linux tr command

1. Introduction tr is used to convert or delete a...