当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
刷题-洛谷-P1200 你的飞碟在这儿Your Ride Is Here
C#的Dictionary字典集合按照key键进行升序和降序排列
QT 小知识随记
前3名突然变了,揭秘 7 月编程语言最新排行榜
The establishment of simple data cache layer
简单理解 JS 事件循环
5G NR 笔记记录
多商户商城系统功能拆解22讲-平台端分销商品
getBoundingClientRect
成品升级程序
腾讯云胡启明:Kubernetes云上资源的分析与优化
对比几类主流的跨端技术方案
MySQL字段类型
June To -.-- -..- -
电脑一键重装系统后连不上远程了?教你设置的方法
图片延迟加载、预加载
web 应用开发最佳实践之一:避免大型、复杂的布局和布局抖动
刷题-洛谷-P1307 数字反转
Desthiobiotin-PEG4-Azide_脱硫生物素-叠氮化物 100mg
Zip4j使用








