前言
大家好吖,欢迎来到 YY 滴MySQL系列 ,热烈欢迎! 本章主要内容面向接触过C++的老铁
主要内容含:
欢迎订阅 YY滴C++专栏!更多干货持续更新!以下是传送门!
一.if判断
1.基本语法
2.利用if判断,定义存储过程,完成如下需求
-- 根据定义的分数score变量,判定当前分数对应的分数等级
-- score >= 85分,等级为优秀。
-- score >=60分 且score<85分,等级为及格。
-- score <60分,等级为不及格。
create procedure p3()
begin
declare score int default 58;
declare result varchar(10);
if score >= 85 then
set result :='优秀';
else if score >= 60 then
set result :='及格';
else
set result :='不及格'
end if;
end;
call p3();
二.存储过程参数(IN,OUT,INOUT)
1.(IN,OUT,INOUT)基本介绍&语法
2.存储过程参数(IN,OUT),定义存储过程,完成如下需求
-- 根据传入(in)参数score,判定当前分数对应的分数等级,并返回(out)
-- score >= 85分,等级为优秀。
-- score >=60分 且score<85分,等级为及格。
-- score <60分,等级为不及格。
create procedure p4(in score int, out result varchar(10))
begin
if score >= 85 then
set result :='优秀';
elseif score >= 60 then
set result :='及格';
else
set result :='不及格';
end if;
end;
--调用
call p4( score: 18, result: @result);
select @result;
3.存储过程参数(INOUT),定义存储过程,完成如下需求
-- 将传入的 200分制的分数,进行换算,换算成百分制,然后返回分数--->inout
create procedure p5(inout score double)
begin
set score := score *0.5;
end;
set @score = 78;
call p5( score: @score);
select @score;
三.case,while,repeat,loop循环
1.case循环语法&定义存储过程完成需求
create procedure p6(in month int)
begin
declare result varchar(10);
case
when month >=1 and month <= 3 then
set result :='第一季度';
when month >=4 and month <= ó then
set result :='第二季度';
when month >=7and month <= 9 then
set result :='第三季度";
when month>=10 and month <= 12 then
set result :='第四季度";
else
set result :='非法参数";
end case ;
select concat('您输入的月份为:',month,',所属的季度为:',result);
end;
call p6(month:4)
2.while循环语法&定义存储过程完成需求
-- A.定义局部变量,记录累加之后的值;
-- B.每循环一次,就会对进行减1,如果n减到日,则退出循环
create procedure p7(in n int)
begin
declare total int default 0;
while n>o do
set total := total + n;
set n:= n-1;
end while;
select total;
end;
call p7(n: 100);
3.repeat循环语法&定义存储过程完成需求
-- A.定义局部变量,记录累加之后的值;
-- B.每循环一次,就会对n进行-1,如果n减到,则退出循环
create procedure p8(in n int)
begin
declare total int default 0;
repeat
set total := total + n;
set n:= n-1;
until n<= 0
end repeat;
select total;
end;
call p8(n: 100);
4.loop循环语法&定义存储过程完成需求
-- A.定义局部变量,记录累加之后的值;
-- B.每循环一次, 就会对进行-1 , 如果n减到, 则退出循环---->leave xx
create procedure p9(in n int)
begin
declare total int default 0;
sum:loop
if n<=0 then
leave sum;
end if;
set total := total + n;
set n:=n-1;
end loop sum;
select total;
end;
call p9(n: 100);
-- A.定义局部变量, 记录累加之后的值;
-- B.每循环一次,就会对进行-1, 如果n减到, 则退出循环----> leave xx
-- c.如果当次累加的数据是奇数, 则直接进入下一次循环.----> iterate xx
create procedure p10(in n int)
begin
declare total int default 0;
sum:Loop
if n<=0 then
Leave sum;
end if;
if n%2 = 1 then
set n:=n-1;
iterate sum;
end if;
set total := total + n;
set n:= n-1;
if n%2 = 1 then
set n:=n-1;
iterate sum;
end if;
set total := total + n;
set n:=n-1;
end loop sum;
select total;
end;
call p10( n: 10);
四.游标cursor
1.游标要解决的【查询问题场景】
-
我们有时往往会采用以下语句查询student表中的 总记录数count(*)
-
当我们想要把student表的 数据全部读出来* 并赋值给count时
-
报错原因:逻辑错误: student表所有数据 赋值给 int变量。而该变量只能接受 单行单列 类型的数据,如果要接受 表/结果集 ,那么用自己定义的变量就不合适了。应该应用游标
2.游标循环语法&定义存储过程完成需求
-- 游标
-- 根据传入的参数uage:来查询用户表 tb_user中,所有的用户年龄小于等于uage的用户姓名(name)和专业(profession)
-- 并将用户的姓名和专业插入到所创建的一张新表(id,name,profession)中。
-- 逻辑:
-- A.声明游标,存查询结果集
-- B.准备:创建表结构
-- C.开启游标
-- D.获取游标中的记录
-- E.插入数据到新表中
-- F.关闭游标
-- 注意事项:局部变量声明要在游标前
create procedure p11(in uage int)
begin
declare upame varchar(100)
declare upro varchar(100);
declare u_cursor cursor for select name,profession from tb_user where age <= uage;
drop table if exists tb_user_pro;
create table if not exists tb_user_pro(
id int primary key auto_increment,
name varchar(100),
profession varchar(100)
);
open u_cursor;
while true do
fetch u_cursor into uname,upro;
insert into tb_user_pro values(null, uname, upro);
end while;
close u_cursor;
end;
- 结果:游标无数据获取,我们可以通过下面第五点 条件处理程序来解决
五.条件处理程序handler
1.mysql中的状态码
- mysql官方文档中有对其进行描述,我们可以直接查询
- 图中查询结果:02000状态码表示抓取数据时无数据
2.条件处理程序语法&定义存储过程完成需求
- 注意: 设计前要进行声明
- 我们要解决的需求是第四点中游标没有完成的需求
- 如下面我们设计的 条件处理程序逻辑 为
- 我们声明一个条件处理程序,当状态码为 ‘02000’ 时触发退出【exit】操作
- 并关闭游标
我们将其写入存储过程中,没有报错,问题解决
- 同时我们注意到NOT FOUNTD 可以表示所有以02卡位i头的SQLSTATE代码,因此我们可以再次改写