跟孙哥学java
介绍:
存储过程是事先经过编译并存储在数据库中的一段SQL语句的集合,调用存储过程可以简化应用开发人员的很多工作,减少数据在数据库和应用服务器之间的传输,对于提高数据处理的效率是有好处的。 存储过程思想上很简单,就是数据库SQL语言层面的代码封装与重用。
特点 封装,复用----->可以把某一业务SQL封装到存储过程中,需要用可以直接调用 可以接收参数,也可以返回数据--->在存储过程中,可以传入参数in,也可以输出返回值out 减少网络交互,效率提升--->如果一个业务涉及多条sql查询,每次执行一条sql都会进行一次网络传输。如果封装到存储过程中,可以减少网络传输的消耗
语法
创建:
create procedure 存储过程的名称(参数)
begin
sql语句;
end;
create procedure p1()
begin
select *from user;
end;
调用:
call 名称(参数);
call p1();
执行结果就是上述创建存储过程中的执行结果
查看存储过程:
select *from information_schema.ROUTINES where ROUTINE_SCHEMA='db01';
-- 查询指定数据库(db01)的存储过程及状态信息
可以看到在db01表中所创建的存储过程以及其信息
若指向查看创建存储过程的语句--->
show create procedure p1;
** 删除:**
drop procedure p1;
删除后再次查询p1--->p1 not exit -->已经被删除了
:::info
小细节:如果是在命令行中执行创建procedure的语句--->会报错
这是因为begin 与end之间的sql语句存在 分号; 系统会认为到分号就结束了
所以我们在创建procedure之间,使用delimirer-->指定sql语句的结束符
:::
变量
在MySQL中,变量分为,系统变量,用户变量,局部变量
系统变量
系统变量又分为:全局变量(GLOBAL),会话变量(SESSION) 1.查看变量
show [session|global] variables; --->查看系统的所有变量
show [session|global] variables like '%'; --->可以通过模糊匹配进行查询
show session variables like 'auto%';
select @@[session|global] 系统变量;
select @@session.autocommit;
2.设置系统变量
set @@session.autocommit=0;
:::info 注意: 如果没有指定global/session 系统默认是session :::
用户自定义变量
用户定义变量 是用户根据需要自己定义的变量,用户变量不用提前声明,在用的时候直接用 "@变量名" 使用就可以。其作用域为当前连接。 赋值/获取值
set @my_name='张三';或者set @my_name:='张三';
select @my_name;
局部变量
局部变量 是根据需要定义的在局部生效的变量,访问之前,需要DECLARE声明。可用作存储过程内的局部变量和输入参数,局部变量的范围是在其内声明的BEGIN ... END块。 1.声明
create procedure p2()
begin
declare i int default 0;
end;
begin--end之间的 i就是定义的局部变量 2.赋值
SET 变量名 = 值 ;
SET 变量名 := 值 ;
SELECT 字段名 INTO 变量名 FROM 表名 ... ;
set i:=1;
create procedure p2()
begin
declare i int default 0;
set i:=1;
select count(*) into i from user;
end;
if
用于做条件判断
语法结构
if 条件 then
....
elseif 条件2 then
...
end if;
在if条件判断的结构中,ELSE IF 结构可以有多个,也可以没有。 ELSE结构可以有,也可以没有。
案例
:::info 根据定义的分数score变量,判定当前分数对应的分数等级。 score >= 85分,等级为优秀。 score >= 60分 且 score < 85分,等级为及格。 score < 60分,等级为不及格。 :::
create procedure p3()
begin
declare score int default 99;
declare result varchar(10);
if score>=85 then
set result :='优秀';
else if score>=60 then
set result:='及格';
else
set result:='不及格';
end if;
end if;
select result;
end;
call p3;
参数
介绍
参数的类型,主要分为以下三种:IN、OUT、INOUT。 具体的含义如下:
| 类型 | 含义 | 备注 |
|---|---|---|
| IN | 该类参数作为输入,也就是需要调用时传入值 | 默认 |
| OUT | 该类参数作为输出,也就是该参数可以作为返回值 | |
| INOUT | 既可以作为输入参数,也可以作为输出参数 |
语法
CREATE PROCEDURE 存储过程名称 ([ IN/OUT/INOUT 参数名 参数类型 ])
BEGIN
-- SQL语句
END ;
:::info 根据传入参数score,判定当前分数对应的分数等级,并返回。 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 :='优秀';
else if score>=60 then
set result:='及格';
else
set result:='不及格';
end if;
end if;
end;
call p4(96,@my_result);
select @my_result;
:::info
将传入的200分制的分数,进行换算,换算成百分制,然后返回。
:::
call p4(96,@my_result);
select @my_result;
drop procedure p5;
create procedure p5(inout score double)
begin
set score:=score*0.5;
end;
set @score=190;
call p5(@score);
select @score;
case
介绍
case结构及作用,和我们在基础篇中所讲解的流程控制函数很类似。有两种语法格式:
语法
-- 含义: 当case_value的值为 when_value1时,执行statement_list1,当值为 when_value2时,执行statement
_
list2, 否则就执行 statement_list
CASE case_value WHEN when_value1 THEN statement_list1
[ WHEN when_value2 THEN statement_list2] ...
[ ELSE statement_list ]
END CASE;
案例
:::info 根据传入的月份,判定月份所属的季节(要求采用case结构)。 1-3月份,为第一季度 4-6月份,为第二季度 7-9月份,为第三季度 10-12月份,为第四季度 :::
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<=6 then
set result='第2季度';
when month>=7 and month<=9 then
set result='第3季度';
when month>=10 and month<=12 then
set result='第4季度';
else
set result='非法参数';
end case ;
select result;
end;
call p6(11);
while
介绍
while 循环是有条件的循环控制语句。满足条件后,再执行循环体中的SQL语句。具体语法为:
-- 先判定条件,如果条件为true,则执行逻辑,否则,不执行逻辑
WHILE 条件 DO
SQL逻辑...
END WHILE;
案例
:::info 计算从1累加到n的值,n为传入的参数值。 :::
create procedure p7(in n int )
begin
declare result int default 0;
while n>0 do
set result=result+n;
set n=n-1;
end while;
select result;
end;
call p7(10);
repeat
介绍
repeat是有条件的循环控制语句, 当满足until声明的条件的时候,则退出循环 。具体语法为:
-- 先执行一次逻辑,然后判定UNTIL条件是否满足,如果满足,则退出。如果不满足,则继续下一次循环
REPEAT
SQL逻辑...
UNTIL 条件
END REPEAT;
案例
:::info 计算从1累加到n的值,n为传入的参数值。(使用repeat实现) :::
create procedure p8(n int)
begin
declare result int default 0;
repeat
set result=result+n;
set n=n-1;
until n<=0
end repeat;
select result;
end;
call p8(11);
loop
介绍
LOOP 实现简单的循环,如果不在SQL逻辑中增加退出循环的条件,可以用其来实现简单的死循环。LOOP可以配合一下两个语句使用: LEAVE :配合循环使用,退出循环。 ITERATE:必须用在循环中,作用是跳过当前循环剩下的语句,直接进入下一次循环。
[begin_label:] LOOP
SQL逻辑...
END LOOP [end_label];
LEAVE label; -- 退出指定标记的循环体
ITERATE label; -- 直接进入下一次循环
案例
:::info 计算从1累加到n的值,n为传入的参数值。 :::
create procedure p9(n int)
begin
declare result int default 0;
sum:loop
if n<=0 then
leave sum;
end if;
set result=result+n;
set n=n-1;
end loop;
select result;
end;
call p9(10);
游标
介绍
游标(CURSOR)是用来存储查询结果集的数据类型 , 在存储过程和函数中可以使用游标对结果集进行循环的处理。游标的使用包括游标的声明、OPEN、FETCH 和 CLOSE,其语法分别如下。
声明游标
DECLARE 游标名称 CURSOR FOR 查询语句 ;
打开游标
OPEN 游标名称 ;
获取游标记录
FETCH 游标名称 INTO 变量 [, 变量 ] ;
关闭游标
CLOSE 游标名称 ;
案例
根据传入的参数uage,来查询用户表tb_user中,所有的用户年龄小于等于uage的用户姓名 (name)和专业(profession),并将用户的姓名和专业插入到所创建的一张新表 (id,name,profession)中。
create procedure p11(in uage int)
begin
declare uname varchar(100);
declare upro varchar(100);
declare u_cursor cursor for select name,profession from tb
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;
call p11(30);
条件处理程序
介绍
条件处理程序(Handler)可以用来定义在流程控制结构执行过程中遇到问题时相应的处理步骤。具体语法为: