数据库中存储过程,看这一篇就够了!!
目录
一、什么是存储过程
创建存储过程的基本语法:
二、变量
用户定义变量 :
局部变量 :
if基本语法:
三、传参
in和out使用
inout的使用
四、case的存储过程运用
五、while的运用
七、LOOP的运用:
八、游标
一、什么是存储过程
介绍
存储过程是事先经过编译并存储在数据库中的一段 SOL语句的集合,调用存储过程可以简化应用开发人员的很多工作,减少数据在数据库和应用服务器之间的传输,对于提高数据处理的效率是有好处的存储过程思想上很简单,就是数据库 SOL 语言层面的代码封装与重用。
有什么特点呢?
封装,复用
可以接收参数,也可以返回数据,减少网络交互,效率提升 。
注意:在命令行中,执行创建存储过程的SQL时,需要通过关键字 delimiter 指定SQL语句的结束符。
创建存储过程的基本语法:
调用存储过程:call 存储过程名();
查看存储过程:show create procedure 存储过程名;
删除存储过程:drop procedure if exists 存储过程名;
案例:
# 创建存储过程
DELIMITER //
create procedure p1()
begin
select count(*) from stu表;
end //
DELIMITER ;
# 调用存储过程
call p1();
# 查看存储过程
select * from information_schema.ROUTINES where ROUTINE_SCHEMA = 'book_sys';
show create procedure p1;
# 删除存储过程
drop procedure if exists p1;
二、变量
存储过程中的变量:系统变量是MYSQL服务器提供,不是用户定义的,属于服务器层面。分为全局变量(GLOBAL)、会话变量(SESSION)
注意:
如果没有指定SESSION/GLOBAL,默认是SESSION,会话变量。
mysql服务重新启动之后,所设置的全局参数会失效,要想不失效,可以在 /etc/my.cnf 中配置。
# 变量
# 查看系统变量,会话级别的
show session variables ;
show session variables like 'auto%';
show global variables like 'auto%';
select @@global.autocommit;
select @@session.autocommit;
# 设置系统变量
set session autocommit =0; #自动提交关闭,需要手动提交
commit ; #手动提交
# 设置全局变量
set global autocommit =0;
select @@global.autocommit;
用户定义变量 :
是用户根据需要自己定义的变量,用户变量不用提前声明,在用的时候直接用“@变量名”使用就可以。其作用域为当前连接。
注意:
用户定义的变量无需对其进行声明或初始化,只不过获取到的值为NULL
局部变量 :
是根据需要定义的在局部生效的变量,访问之前,需要DECLARE声明。可用作存储过程内的局部变量和输入参数,局部变量的范围是在其内声明的BEGIN…END块。
案例:
# 局部变量,定义
create procedure p2()
begin
declare stu_count int default 0;
select count(*) into stu_count from stu表;
select stu_count;
end;
call p2();
if基本语法:
案例:
#根据定义的分数score变量,判定当前分数对应的分数等级。- - # score >=85分,等级为优秀。 # score>=60分日score<85分,等级为及格。 # score< 6日分,等级为不及格。
#根据定义的分数score变量,判定当前分数对应的分数等级。- -
# score >=85分,等级为优秀。
# score>=60分日score<85分,等级为及格。
# score< 6日分,等级为不及格。
create procedure p3()
begin
# 定义了变量,定死了,不能改变,如果要改变参数,则在p3()里面传参
declare score int default 60;
declare result varchar(10);
if score >=85 then
set result:='优秀';
elseif score>=60 then
set result:='及格';
else
set result:='不及格';
end if;
select result;
end;
call p3();
三、传参
案例:
in和out使用
# 根据传入参数score,判定当前分数对应的分数等级,并返回
# score >= 85分,等级为优秀。
# score>=60分且score<85分,等级为及格
# score<60分,等级为不及格。
create procedure p4(in score int,out result varchar(10))
begin
# 定义了变量,定死了,不能改变,如果要改变参数,则在p3()里面传参
# declare score int default 60;
# declare result varchar(10);
if score >=85 then
set result :='优秀';
elseif score>=60 then
set result :='及格';
else
set result :='不及格';
end if;
# select result;
end;
call p4(99,@result);
select @result;
inout的使用
# 将传入的 200分制的分数,进行换算,换算成百分制,然后返回分数--inout既是输入也是输出
create procedure p5(inout score double)
begin
set score = score*0.5;
end;
# 进行复值
set @result=99.9;
# 赋值完后调用分数,进行换算,换算完返回给result
call p5(@result);
select @result;
四、case的存储过程运用
# case
# 根据传入的月份,判定月份所属的季节(要求采用case结构)
# --1-3月份,为第一季度
# --4-6月份,为第二季度
# --7-9月份,为第三季度
# 10-12月份,为第四季度
create procedure p6(in month int)
begin
declare reusult varchar(10);
case
when month>=1 and month <=3 then
set reusult :='第一季度';
when month >=4 and month <=6 then
set reusult :='第二季度';
when month >=7 and month <=9 then
set reusult :='第三季度';
when month >=10 and month <=12 then
set reusult :='第四季度';
else
set reusult :='非法参数';
end case;
select concat('你输入的月份为:',month,'所属为:',reusult);
end;
call p6(13);
运行结果:
五、while的运用
while 循环是有条件的循环控制语句。满足条件后,再执行循环体中的SQL语句。具体语法为:
# while计算从1累加到n的值:n为传入的参数值-
# --A.定义局部变量,记录累加之后的值;
# B.每循环一次,就会劝进行减1,如果n减到0,则退出循环
create procedure p7(in n int)
begin
declare total int default 0;
while n>0 do
set total:=total+n;
set n :=n-1;
end while;
select total;
end;
call p7(100);
运行结果:
七、LOOP的运用:
LOOP 实现简单的循环,如果不在SQL逻辑中增加退出循环的条件,可以用其来实现简单的死循环。LOOP可以配合一下两个语句使用:
LEAVE:配合循环使用,退出循环。
ITERATE:必须用在循环中,作用是跳过当前循环剩下的语句,直接进入下一次循环。
# 计算从1累加到n的值:n为传入的参数值。-- Loop
# --A.定义局部变量,记录累加之后的值;
# B.每循环一次,就会劝进行减1,如果n减到0,则退出循环
create procedure p8(in n int)
begin
declare totle int default 0;
sum:loop
if n <= 0 then
leave sum ;
end if;
set totle :=totle+n;
end loop sum;
select totle;
end;
call p8(10);
# A.定义局部变量,记录累加之后的值;
# B.每循环一次,就会y进行-1,如果n减到,则退出循环 ----> leave xx
# c.如果当次累加的数据是奇数,则直接进入下一次循环.--------> iterqte XX
create procedure p9(in n int)
begin
declare totle int default 0;
sum:loop
if n <= 0 then
leave sum ;
# 在loop中通过leave退出循环
end if;
if n %2 = 1 then
set n:=n-1;
iterate sum;
end if;
set totle :=totle+n;
set n:=n-1;
end loop sum;
select totle;
end;
# 调用1-10之间偶数的和
call p9(100);
八、游标
游标(CURSOR)是用来存储查询结果集的数据类型,在存储过程和函数中可以使用游标对结果集进行循环的处理。游标的使用包括游标的声明、OPEN、FETCH 和 CLOSE,其语法分别如下。
# -- 游标
# 根据传入的参数uage,来查询用户表tb_user中,所有的用户年龄小于等于uage的用户姓名(name)和专业(profession),、
# 并将用户的姓名和专业插入到所创建的一张新表(id,name,profession)中。
-- 逻辑:
# --A.声明游标,存储查询结果集
# --B.准备:创建表结构
# --C.开启游标
# --D.获取游标中的记录
# --E.插入数据到新表中
# --F.关闭游标
create procedure p12(in uage int)
begin
# 先声明普通变量再声明游标变量,定义游标记录条件的结果集
declare uname varchar(10);
declare phone char(11);
declare u_cursor cursor for select tb_user.name,tb_user.phone from tb_user where age <=uage;
# 声明条件处理程序,当满足SQL状态码是是多少时,退出循环,关闭游标
declare exit handler for SQLSTATE '02000' close u_cursor;
# 先输出tb_user_pro表,以防之前有这个表
drop table if exists tb_user_pro;
create table if not exists tb_user_pro(
id int primary key auto_increment,
name varchar(10),
phone char(11)
)comment '创建新表';
# 开启游标
open u_cursor;
while true do
# 获取游标数据,把获取的数据插入到新表中
fetch u_cursor into uname,phone;
insert into tb_user_pro values (null,uname,phone);
end while;
close u_cursor;
end;
call p12(50);
运行结果:
作者:IT小庄同学