本教程中所使用的数据库的建表语句都在“SQL教程——索引”这篇文章中,点击链接直达:索引&建表语句
摘要:本文主要介绍SQL语法的存储过程
目录
存储过程和函数
存储过程和函数:类似java中的方法
好处:
-
提高代码的重用性
-
简化操作
存储过程
含义:一组预先编译好的SQL语句的集合,理解成批处理语句
一、创建语法
create procedure 存储过程名(参数列表)
begin
存储过程体(一组合法的SQL语句)
end
注意:
1、参数列表包含三部分
参数模式 参数名 参数类型
举例:
in stuname varchar(20)
参数模式:
in:该参数可以作为输入,也就是该参数需要调用方传入值
out:该参数可以作为输入,也可以作为返回值
inout:既可以作为输入又可以作为输出,也即该参数需要调用方传入值,又可以当成返回值
2、如果存储过程提仅仅有一句话,begin end可以省略
存储过程的每条SQL语句的结尾要求必须加分号。
存储过程的结尾可以使用 delimiter 重新设置
语法:
delimiter 结束标记
二、调用语法
call 存储过程名(实参列表)
#1.空参列表
#案例1:查处到admin表中5条数据
delimiter $
create procedure myp1()
begin
insert into admin(username, 'password') values ('john1', '0000'), ('john1', '0000'), ('john1', '0000'), ('john1', '0000'), ('john1', '0000'),;
end $
#调用
call myp1() $
#2.创建in模式参数的存储过程
#案例1:创建存储过程,根据女神名,查询对应男神信息
create procedure myp2(in beautyName varchar(20))
begin
select bo.*
from boys bo
right join beauty b
on bo.id = b.boyfriend_id
where b.name = beautyName;
end $
#调用
call myp2('柳岩')$
#3.创建out模式参数的存储过程
#案例1:根据女神名,返回对应男神名
create procedure myp3(in beautyName varchar(20), out boyName varchar(20))
begin
select bo.boyName into boyName
from boys bo
right join beauty b
on b.boyfriend_id = bo.id
where b.name = beautyName;
end $
set @bName = 3 $ #可以不写
call myp3('赵敏', @bName) $
select @bName $
#案例2:根据女神名,返回对应男神名和男神魅力值
create procedure myp4(in beautyName varchar(20), out boyName varchar(20), out userCP int)
begin
select bo.boyName, bo.userCP into boyName, userCP
from boys bo
right join beauty b
on b.boyfriend_id = bo.id
where b.name = beautyName;
end $
#调用
call myp4('小昭', @bName, @userCP);
select @bName, @userCP;
#4.创建intout模式参数的存储过程
#案例1:传入a和b两个值,最终a和b都翻倍并返回
create procedure myp5(inout a int, inout b int)
begin
set a = a * 2;
set b = b * 2;
end $
#调用
set @a = 3$
set @b = 4$
call myp5(@a, @b)$
select @a, @b$
#Test1:创建存储过程实现传入用户名和密码,插入到admin表中
create procedure newUser(in userName varchar(20), in password varchar(20))
begin
insert into admin(username, password) values(userName, password);
end $
#调用
call newUser('肖航', '844940114')$
select * from admin$
#Test2:创建存储过程或函数实现传入女神编号,返回女神名称和女神电话
create procedure searchBu(in beautyId int, out name varchar(20), out phone varchar(20))
begin
select b.name, b.phone into name, phone
from beauty b
where b.id = beautyId;
end $
#调用
set @beautyId = 2$
call searchBu(@beautyId, @name, @phone)$
select @name, @phone$
三、删除语法
drop procedure p1;
drop procedure p1, p2;
四、查看存储过程信息
desc myp2$ # XXX Table 'girls.myp2' doesn't exist
show create procedure myp2$
#Test3:创建存储过程或函数实现传入一个日期,格式化成xx年xx月xx日并返回
create procedure dateToStr(in da date, out daStr varchar(20))
begin
select date_format(da, '%y年%c月%d日') into daStr;
end$
#调用
set @da = curdate()$
call dateToStr(@da, @daStr)$
select @daStr$
#Test4:创建存储过程或函数实现传入女神名称,返回:女神 AND 男神 格式的字符串
create procedure sheHim(inout she varchar(20))
begin
declare him varchar(20);
select b.name, bo.boyname into she, him
from beauty b
left join boys bo
on b.boyfriend_id = bo.id
where b.name = she;
set she = concat(she, ' AND ', him);
end $
#调用
set @she = '小昭'$
call sheHim(@she)$
select @she$