mysql的存储过程的参数_mysql数据存储过程参数实例详解

MySQL 存储过程参数有三种类型:in、out、inout。它们各有什么作用和特点呢?

一、MySQL 存储过程参数(in)

MySQL 存储过程 “in” 参数:跟 C 语言的函数参数的值传递类似, MySQL 存储过程内部可能会修改此参数,但对 in 类型参数的修改,对调用者(caller)来说是不可见的(not visible)。

drop procedure if exists pr_param_in;

create procedure pr_param_in

(

in id int -- in 类型的 MySQL 存储过程参数

)

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 |

+--------+

可以看到:用户变量 @id 传入值为 10,执行存储过程后,在过程内部值为:11(id_inner),但外部变量值依旧为:10(id_out)。

二、MySQL 存储过程参数(out)

MySQL 存储过程 “out” 参数:从存储过程内部传值给调用者。在存储过程内部,该参数初始值为 null,无论调用者是否给存储过程参数设置值。

drop procedure if exists pr_param_out;

create procedure pr_param_out

(

out id int

)

begin

select id as id_inner_1; -- id 初始值为 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 |

+--------+

可以看出,虽然我们设置了用户定义变量 @id 为 10,传递 @id 给存储过程后,在存储过程内部,id 的初始值总是 null(id_inner_1)。最后 id 值(id_out = 1)传回给调用者。

三、MySQL 存储过程参数(inout)

MySQL 存储过程 inout 参数跟 out 类似,都可以从存储过程内部传值给调用者。不同的是:调用者还可以通过 inout 参数传递值给存储过程。

drop procedure if exists pr_param_inout;

create procedure pr_param_inout

(

inout id int

)

begin

select id as id_inner_1; -- id 值为调用者传进来的值

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 |

+--------+

从结果可以看出:我们把 @id(10),传给存储过程后,存储过程最后又把计算结果值 11(id_inner_3)传回给调用者。 MySQL 存储过程 inout 参数的行为跟 C 语言函数中的引用传值类似。

通过以上例子:如果仅仅想把数据传给 MySQL 存储过程,那就使用“in” 类型参数;如果仅仅从 MySQL 存储过程返回值,那就使用“out” 类型参数;如果需要把数据传给 MySQL 存储过程,还要经过一些计算后再传回给我们,此时,要使用“inout” 类型参数。

总结

以上就是本文关于mysql数据存储过程参数实例详解的全部内容,希望对大家了解MySQL有所帮助。感兴趣的朋友可以继续参阅本站:解析MySQL中存储时间日期类型的选择问题、MySQL 声明变量及存储过程分析等,有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL 存储过程是一种封装了 SQL 语句的代码块,可以在 MySQL 数据库中创建、存储和调用。存储过程可以接受输入参数、输出参数和返回值,可以使用分支、循环、异常处理等复杂的编程语言结构,适用于处理复杂的数据操作和业务逻辑。 下面是一个简单的 MySQL 存储过程实例: ```mysql DELIMITER // CREATE PROCEDURE sp_get_customer(IN customer_id INT, OUT customer_name VARCHAR(50)) BEGIN SELECT customer_name INTO customer_name FROM customers WHERE customer_id = customer_id; END // DELIMITER ; ``` 这个存储过程名为 `sp_get_customer`,有一个输入参数 `customer_id` 和一个输出参数 `customer_name`。存储过程的作用是根据传入的 `customer_id` 查询 `customers` 表中对应的客户名,并将其赋值给输出参数 `customer_name`。 使用以下语句调用这个存储过程: ```mysql CALL sp_get_customer(1, @customer_name); SELECT @customer_name; ``` 这个语句会查询 `customers` 表中 `customer_id` 为 1 的客户名,并将其赋值给变量 `@customer_name`,然后输出变量的值。 存储过程可以简化复杂的数据操作和业务逻辑,提高数据库的性能和安全性。但是,存储过程也有一些限制和注意事项: 1. 存储过程的语法比较复杂,需要熟悉 MySQL 的编程语言和语法规则。 2. 存储过程可能会影响数据库的性能和可维护性,应该谨慎使用和设计。 3. 存储过程可能会影响数据库的安全性,应该限制存储过程的访问权限和操作权限。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值