当前位置:网站首页>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,@dd.结果:调用一次,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
边栏推荐
- really time ntp service start command
- nr part calculation
- JS手写JSON.stringify() (面试)
- Elastic Search 根据匹配分和热度分排序
- SAP UI5 确保控件 id 全局唯一的实现方法
- The book "The Essence of Alipay Experience Design", a record of knowledge related to testing
- Chrome安装zotero connector 插件
- 《支付宝体验设计精髓》一书,跟测试相关性知识记录
- 奥拉时钟芯片生成配置文件脚本
- 【Attention 演变史】RNN的产生、架构、推广、问题(第一弹)
猜你喜欢

Qt Designer生成的图形可以自适应窗口的大小变化

2022年国内手机满意度榜单:华为稳坐国产品牌第一

The list of Kubernetes - watch mechanism

Use "green computing" technology to promote sustainable development of computing power

刷题-洛谷-P1307 数字反转

linkboy 5.0 正式发布,新增语音识别、图像识别

前3名突然变了,揭秘 7 月编程语言最新排行榜

零知识证明——zkSNARK证明体系

【Attention 演变史】RNN的产生、架构、推广、问题(第一弹)

刷题-洛谷-P1319 压缩技术
随机推荐
在vs code中进行本地调试和开启本地服务器
数据安全解决方案的发展
The difference between Client Side Cache and Server Side Cache
【Web漏洞探索】跨站脚本漏洞
Embrace the Cmake child is simple and practical, but inflexible
密码学系列之:PEM和PKCS7,PKCS8,PKCS12
图片延迟加载、预加载
Differences in the working mechanism between SAP E-commerce Cloud Accelerator and Spartacus UI
「 WAIC 2022 · 黑客马拉松」蚂蚁财富两大赛题邀你来战!
The book "The Essence of Alipay Experience Design", a record of knowledge related to testing
用“绿色计算“技术推动算力可持续发展
C语言基础[通俗易懂]
小软件大作用 | 如何省时省力进行Gerber图层快速对比?
In July 2022, domestic database memorabilia
C#的Dictionary字典集合按照key键进行升序和降序排列
Apache服务器配置多个站点
使用 Chrome 开发者工具的 lighthouse 功能分析 web 应用的性能问题
awk statistical difference record
QT 小知识随记
Desthiobiotin-PEG4-Azide_脱硫生物素-叠氮化物 100mg