1、存储过程
1)、基本概念
基本语法:
create procedure 存储过程名字([参数列表])
begin
sql语句集
end;
使用delimiter进行修改定义储存过程的结束符默认为;
delimiter $
create procedure 存储过程名字([参数列表])
begin
sql语句集
end $
delimiter ;
定义完存储过程记得将结束符修改回来
查看数据中的存储过程
show procedure status where db=数据库名称
调用存储过程:
call 储存过程名字
删除存储过程
drop procedure 存储过程名字;
2)、存储过程的变量
变量的分类
MySQ变量大体可以分为4中:
局部变量、用户变量、会话变量、全局变量
局部变量
在begin到end语句块之间,在该语句块里设置的变量
作用范围:局部变量只在begin-end语句块之间有效。
一般使用declare来声明
用户变量
用户自定义的变量就叫用户变量。
作用范围:
用户变量跟mysql客户端是绑定的,设置的变量,只对当前用户使用的客户端生效。
当当前连接断开后,其所定义的用户变量都会消失。
以”@“开始,形式为”@变量名" 例如:@test
会话变量
从打开客户端连接到mysql服务器到关闭连接的过程,我们称为一个会话
只对连接的客户端有效。
客户端只能更改自己的会话变量,而不能更改其它客户端的会话变量。
会话变量的作用域与用户变量一样,仅限于当前连接。
当当前连接断开后,其设置的所有会话变量均失效。
对所有客户端生效。只有具有super权限才可以设置全局变量
作用范围:
全局变量作用于服务器的整个生命周期,但是不能重启。即重启后所有设置的全局变量均失效。
要想让全局变量重启后继续生效,需要更改相应的配置文件。
注意:
变量之间的层次关系是:变量包括局部变量和用户变量。
用户变量包括会话变量和全局变量。
declare 局部变量
基本语法:
declare 变量名 数据类型 [default 默认值];
注意:
默认值可以被指定为一个表达式或者常数,
如果没有default子句,初始值为NULL(请记得一定要初始化变量)。
变量set 语句
语法:
set 变量名 = 值||表达式
set 变量名 := 值||表达式 //推荐写法 用于区分 判断两个值是否相等的=号
select into 语句
语法:
select 列名1,列名2,..列名N into 变量1,变量2,..变量N
from 表名 where 过滤条件
注意:
只有单一的一行可以取回
3)、存储过程的参数
在存储过程中的参数列表中可以存放3种参数类型:
1、in输入参数(默认)
2、out输出参数
3、inout输入输出参数
in
示例:通过传入的id 查询学生
create procedure p_show_student(in s_id int)
begin
select * from t_student where id = s_id;
end
call p_show_student(1);
out
示例:传入两个值返回和
create procedure p_show_result(in num1 int,in num2 int , out res int )
begin
set res := num1 + num2;
end
set @res = 0,@num1 = 1,@num2=2;
call p_show_result(@num1,@num2,@res);
select @res from dual;
inout
示例:传入一个整数,并返回它的5倍的值
create procedure p_test_inout(inout num int )
begin
set num := num * 5;
end
set @num = 5;
call p_test_inout(@num);
select @num from dual;
2、流程控制(if)
基础语法:
//单分支
if 条件表达式 then
sql语句集
end if
//双分支
if 条件表达式 then
sql语句集
else
sql语句集
end if
//多分支
if 条件表达式 then
sql语句集
elseif 条件表达式 then
sql语句集
else
sql语句集
end if
注意:
在sql中判断两个值是否相等使用 = 号
赋值使用 = 或者 :=
示例:创建一个测试if-elseif-else的存储过程
create procedure p_testif_elseif_else(in num int)
begin
if num<10 then
select '你输入的数字小于10';
elseif num<20 then
select '你输入的数字小于20';
else
select '你输入的数字大于20';
end if
end
3、流程控制(case 语句 )
基础语法:
- 在存储过程中
//语法一
case 表达式
when 表达式值 then sql语句集;
when 表达式值 then sql语句集.....
[else sql语句集]
end case
//语法二
case
when 条件表达式 then sql语句集;
[when 条件表达式 then sql语句集]……
[else sql语句集]
end case
示例:判断一个数
//方式一
create procedure pro_testCase(in num int)
begin
case num
when 0 then select '你输入的数字等于0';
when 1 then select '你输入的数字等于1';
else select '你输入的数字不等于1也不等于0';
end case
end
//方式二
create procedure pro_testCase(in num int)
begin
case
when num <10 then select '你输入的数字小于10';
when num>10 then select '你输入的数字大于10';
else select '你输入的数字等于10';
end case
end
- 在普通的sql语句中:
//语法一
select 列名列表,case 值 when 比较值 then 结果
[when 比较值 then 结果….]
[else 结果] end
from 表名 where 过滤条件
//语法二
select 列名列表,case when 条件表达式 then 结果
[when 条件表达式 then 结果….]
[else 结果] end
from 表名 where 过滤条件
示例1:查询直接显示性别 0女 1男
select name as '姓名',
( case gender
when 0 then '女'
when 1 then '男'
end ) as '性别'
from studentSubjectScore;
示例2:查询成绩并显示成绩情况
select name as '姓名',subject as '科目',
( case when score >=90 and score<=100 then '优秀'
when score >=70 and score<90 then '良好'
when score >=60 and score<70 then '合格'
else '差'
end ) as '科目成绩'
from studentSubjectScore;
4、流程控制循环
1)、while
基本语法:
while 条件表达式 do
sql语句集
end while
示例: 创建一个存储过程(pro_while), 输入一个整数(num),计算1+2+3+….num的和。
create procedure p_while(in num int)
begin
declare sum int default 0;
declare i int default 0;
while i <= num do
set sum := sum + i;
set i := i + 1;
end while
select sum as '计算结果';
end
2)、repeat
基本语法:
repeat
sql语句集
until 条件表达式 end repeat
示例: 创建一个存储过程(pro_while), 输入一个整数(num),计算1+2+3+….num的和。
create procedure p_repeat(in num int)
begin
declare sum int default 0;
declare i int default 0;
repeat
set sum := sum + i;
set i := i + 1;
until i <= num end repeat;
select sum as '计算结果';
end
3)、loop
基本语法:
begin_label: loop
sql语句集
end loop :end_label
示例: 创建一个存储过程(pro_while), 输入一个整数(num),计算1+2+3+….num的和。
create procedure p_repeat(in num int)
begin
declare sum int default 0;
declare i int default 0;
beginLoop: loop
set sum := sum + i;
set i := i + 1;
if i <= num then
leave beginLoop;
end if
end loop beginLoop;
select sum as '计算结果';
end