当前位置:网站首页>MySQL stored procedure introduction, creation, case, delete, view "recommended collection"
MySQL stored procedure introduction, creation, case, delete, view "recommended collection"
2022-08-04 20:04:00 【Full stack programmer webmaster】
大家好,又见面了,我是你们的朋友全栈君.
存储过程
含义: 一组预先编译好的sql语句的集合,理解成批处理语句 好处: 1、提高代码的重用性 2、简化操作 3、减少了编译次数并且减少了和数据库服务器的连接次数,提高了效率
一、创建语法
create procedure 存储过程的名字(参数列表)
BEGIN
存储过程体(一组合法有效的sql语句)
END
注意: 1.参数列表包含三部分
参数模式 参数名 参数类型
举例:
IN stauname varchar(20)
参数模式:
IN :该参数可以作为输入,也就是该参数需要调用方 传入值
OUT :该参数可以作为输出,也就是该参数可以作为返回值
INOUT:该参数既可以作为输入也可以作为输出,也就是该参数既需要传入值,又可以返回值
2.如果存储过程体仅仅只有一句话,BEGIN END 可以省略
3.存储过程体中的每条sql语句的结尾必须加分号.
4.存储过程的结尾可以使用delimiter重新设置
语法:
delimiter 结束标记
案例:
delimiter $
二、调用语法:
CALL 存储过程名(实参列表);
三、实战:
1.空参列表
案例:
要求:向boys表中插入数据
select * from boys;
a.创建存储过程:
delimiter $
create procedure mypro1()
begin
INSERT into boys VALUES(5,'张三',1223);
INSERT into boys VALUES(6,'张6',1233);
INSERT into boys VALUES(7,'张7',1243);
INSERT into boys VALUES(8,'张8',1253);
END $
b.调用存储过程:
call mypro1();
2.创建in模式参数的存储过程
案例:
要求:创建存储过程实现,根据女生名,Find the corresponding male information
a.创建存储过程:
delimiter $
create procedure mypro3 (IN girlname varchar(20) )
BEGIN
select * from boys b right join beauty g
ON b.id=g.boyfrind_id
where g.name = girlname;
END $
b.调用存储过程:
call mypro3('刘岩')
call mypro3('小昭')
进阶版,利用 if 函数,返回‘成功’、‘失败’;
结果:
3.创建out模式参数的存储过程
案例1:根据女生名,返回对应的男生名 (以mysql8.0版本为例,和mysql5.5It's slightly different when called)
a. 创建存储过程
delimiter $#Defines the symbol for the end of the stored procedure
create procedure mypro4(IN girlname varchar(20),OUT mingzi varchar(20))
BEGIN
select b.boyname INTO mingzi#The result of the query is given to the variablemingzi进行赋值
FROM
beauty g JOIN boys b
ON g.boyfrind_id=b.id
where g.NAME=girlname;#Make conditional girls namesg.NAMEEqual to the value passed in when calling the stored procedure
END $
b.调用存储过程
#@bNameis the set accepting variable,用来放out模式的mingzi返回的值
CALL mypro4('小昭',@bName);
#Query variables directly,便可以得到结果,这是8.0版本的写法,5.Several versions need to end with a defined terminator
select @bName
案例2: This case uses two beltsout参数的返回值,用的是mysql5.5的版本
最后进行查看:select @bName,@usercp$ (mysql8.0,Do not use a custom end symbol when calling,mysql5.5A custom end symbol is required)
4.创建带inout模式参数的存储过程 案例1:传入a和b两个值,最终a和b都翻倍并返回
a.创建
delimiter $
create PROCEDURE mypr05(inout a int,INOUT b int)
BEGIN
SET a=a*2;
SET b=b*2;
end $
b.调用
#这里不能直接调用,否则返回的a,bThe value of the variable has nowhere to store,
#So two variables need to be set,赋初值,用c,dvariable to pass in the initial value,Then receive the new value returned
#First define two user variables,to store the returneda,b变量值
SET @c=2;
SET @d=10;
call mypr05(@c,@d)
c.查看
#查看c,d的值,That is, after the call,a,b的返回值
select @c,@d
d.结果:调用一次,double once
5.A case study of stored procedures
四、存储过程的删除
==Only one stored procedure name can be deleted at a time == 语法:
drop procedure 储存过程名
五、View stored procedures
语法:
show create procedure 存储过程名
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/106440.html原文链接:https://javaforall.cn
边栏推荐
- Use "green computing" technology to promote sustainable development of computing power
- 【CAS:2306109-91-9 |胺-PEG4-脱硫生物素】价格
- [Awards for Essays] Autumn recruitment special training to create your exclusive product experience
- really time ntp服务启动命令
- C#移动OA办公系统源码(基于微信企业号)
- 零知识证明笔记——私密交易,pederson,区间证明,所有权证明
- awk 统计平均 最大 最小值
- 用“绿色计算“技术推动算力可持续发展
- 华为WLAN技术:AP上线及相关模板的配置实验
- SAP UI5 ensures that the control id is globally unique implementation method
猜你喜欢
基于Nodejs的电商管理平台的设计和实现
面试官:JVM运行时数据区包含哪几部分?作用是啥?
使用 Allatori 进行 Jar 包混淆
Go学习笔记(篇一)配置Go开发环境
How to monitor code cyclomatic complexity by refactoring indicators
C#移动OA办公系统源码(基于微信企业号)
Quantitative trading robot system development
The book "The Essence of Alipay Experience Design", a record of knowledge related to testing
In July 2022, domestic database memorabilia
Qt Designer生成的图形可以自适应窗口的大小变化
随机推荐
IIC驱动OLED
搭建MyCat2双主双从的MySQL读写分离
vscode离线安装插件方法
刷题-洛谷-P1319 压缩技术
Aura clock chip generation configuration file script
使用.NET简单实现一个Redis的高性能克隆版(二)
简单理解 JS 事件循环
Differences in the working mechanism between SAP E-commerce Cloud Accelerator and Spartacus UI
【Attention演变史】翻译模型seq2seq (第二弹)
Apache服务器的配置[通俗易懂]
Elastic Search 根据匹配分和热度分排序
mysql的存储过程介绍、创建、案例、删除、查看「建议收藏」
二叉树的前序遍历
前3名突然变了,揭秘 7 月编程语言最新排行榜
关于 SAP 电商云 Spartacus UI SSR 的 state transfer 问题
Embrace the Cmake child is simple and practical, but inflexible
使用百度EasyDL实现森林火灾预警识别
完善的交叉编译环境记录 peta 生成的shell 脚本
使用 Allatori 进行 Jar 包混淆
vehemently condemn