MySQL - 补充 - 存储过程

MySQL - 补充 - 存储过程篇

含义

存储过程:是保存在一条或多条SQL的批处理脚本

优点

  • 存储过程可以把复杂逻辑封装在一个单元中

  • 存储过程可以回传值,并可以接受参数

  • 存储过程可以用在数据检验和业务逻辑实现

基本语法

drop procedure if exists x1;
delimiter // 
create procedure x1()
begin
sql语句
end//
delimiter;

call x1();

in

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RzCgwfhV-1651638742168)(springboot.assets/image-20220504114841061.png)]

call x1(5);

那么输出就是

5

示例表

select * from t_1;
id n1 n2
1  a  b
2  aa
3     bbb

上代码

#更改SQL语句的结束符号位$$
delimiter $$
drop procedure if exists pro_seclet;
create procedure pro_select(in v_id int)
begin
	select * from t_1 where id = v_id;
end
&&
delimiter;

#调用存储过程
call pro_select(2);
id n1 n2
2  aa

call pro_select(1);
id n1 n2
1  a   b

自我总结

就是一个函数(方法)

md,让我看半天文字

细节

写法

  • 存错过程不能一行写下来,要换行

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HbR7fVre-1651638742171)(springboot.assets/image-20220504110324010.png)]

变量

不加@会报错

全局变量

set global xxx = xx;

set @@xxx = xx;

用户变量

set @aa = 1;

局部变量

declare aa int default 0;

set aa = 1;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鬼鬼骑士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值