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)]](https://img-blog.csdnimg.cn/18ddc1b5356a49719a0f07f1373cb376.png)
call x1(5);
那么输出就是
5
示例表
select * from t_1;
id n1 n2
1 a b
2 aa
3 bbb
上代码
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)]](https://img-blog.csdnimg.cn/b219988935c24242bf95d27ba2d2a18a.png)
变量
不加@会报错
全局变量
set global xxx = xx;
set @@xxx = xx;
用户变量
set @aa = 1;
局部变量
declare aa int default 0;
set aa = 1;
|