介绍
对sql语句进行封装、复用
创建、调用
--存储过程
--创建
create procedure p1()
beginselect count(*) from t_test;end;--调用
call p1();
存储过程查看、删除
--查看
select * from information_schema.ROUTINES WHERE ROUTINE_SCHEMA = 'test'
SHOW create procedure p1;
删除
--删除
drop procedure if exists p1;
注意
在命令行中,执行创建存储过程的sql时,需要通过关键字delimiter指定sql语句的结束符
eg:
delimiter $$
存储过程-- 变量
系统变量时mysql服务器提供,不是用户定义的,属于服务器层面。分为全局变量(GLOBAL)、会话变量(SESSION)。
注意这里:这样的:@@a表示系统变量名!
--查看系统变量
show variables;
show session variables;
show session variables like 'auto%';
show global variables like 'auto%';
select @@autocommit;
select @@session.autocommit;
select @@global.autocommit;--设置系统变量
set session autocommit = 1;
set global autocommit = 1;insert into test_v_1 values(4,'Tom');
commit;
存储过程–用户自定义变量
自定义变量: 时用户根据需要自己定义的变量,用户变量不用提前声明,在用的时候直接用“@变量名”使用就可以。其作用域为当前连接
两个@@为系统变量,一个@为用户自定义变量!
用户自定义的变量无需声明或初始化,只不过获取到的置为null。
注意这里的赋值语句:可以用 =或:=,介意使用:=,因为存储过程中=既是赋值语句也是对比语句
-- 用户变量:赋值set @myname = 'itcast';
set @myage := '10';
set @mygender := '男',@myhobby := 'java';select @mycolor := 'red';
select count(*) into @mycount from t_test;-- 使用
select @myname,@myage,@mygender,@myhobby;
select @mycolor,@mycount;
存储过程–局部变量
局部变量 是根据需要定义的在局部生效的变量,访问之前,需要DECLARE声明。可用作存储过程内的局部变量和输入参数,局部变量的范围是在其内声明的BEGIN…END快。
-- 局部变量
create procedure p2()
begindeclare stu_count int default 0;select count(*) into stu_count from t_test;select stu_count;end;call p2();
存储过程–流程控制
存储过程–if
eg:
create procedure p3()
begindeclare score int default 58;declare result varchar(10);if score >= 85 thenset result := '优秀';elseif score >= 60 thenset result := '及格';elseset result := '不及格';end if;select result;end;call p3();
存储过程–参数
eg:
create procedure p4(in score int,out result varchar(10))
beginif score >= 85 thenset result := '优秀';elseif score >= 60 thenset result := '及格';elseset result := '不及格';end if;end;call p4(98,@result);
select @result
create procedure p5(inout score double)
beginset score := score * 0.5;end;set @score := 198;
call p5(@score);
select @score;
存储过程–case
create procedure p6(in month int)
begindeclare result varchar(10);casewhen month >= 1 and month <= 3 thenset result := '第一季度';when month >= 4 and month <= 6 thenset result := '第二季度';when month >= 7 and month <= 9 thenset result := '第三季度';when month >= 10 and month <= 12 thenset result := '第四季度';elseset result := '非法参数';end case;select concat('你输入的月份为:',month,',所属的季度为:',result);end;call p6(4);
存储过程–while
create procedure p7(in n int)
begindeclare total int default 0;while n>0 doset total := total +n;set n := n -1;end while;select total;
end;call p7(100);
存储过程–repeat
repeat第一次不管是否满足条件,都会执行,满足条件,则退出循环
create procedure p8(in n int)
begindeclare total int default 0;repeat set total := total + n;set n:= n - 1;until n < 0end repeat;select total;
end;
call p8(10);
call p8(100);
存储过程–loop
create procedure p9(in n int)
begindeclare total int default 0;sum:loopif n<= 0 thenleave sum;end if;set total := total + n;set n := n -1;end loop sum;select total;
end;
call p9(100);
create procedure p10(in n int)
begindeclare total int default 0;sum:loopif n<= 0 thenleave sum;end if;if n % 2 = 1 thenset n := n -1;iterate sum;end if;set total := total + n;set n := n -1;end loop sum;select total;
end;call p10(100);
存储过程–游标、条件处理程序
游标是可以存查询出来结果集的,相当于保存集合
注意:先声明普通变量,再声明游标
条件处理程序
– 条件处程序,当满足条件sql状态码为02000是触发:关闭游标,退出存储过程
declare exit handler for SQLSTATE ‘02000’ close u_cursor;
create procedure p11(in uid int)
begindeclare uname varchar(100);declare uno int;declare u_cursor cursor for select name,no from t_test where id < uid;-- 条件处程序,当满足条件sql状态码为所有以02开头的触发:关闭游标,退出存储过程declare exit handler for SQLSTATE '02000' close u_cursor;drop table if exists tb_user_pro;create table if not exists tb_user_pro(id int primary key auto_increment,name varchar(100),no varchar(100));open u_cursor;while true dofetch u_cursor into uname,uno;insert into tb_user_pro values (null,uname,uno);end while;close u_cursor;
end;call p11(10);
create procedure p12(in uid int)
begindeclare uname varchar(100);declare uno int;declare u_cursor cursor for select name,no from t_test where id < uid;-- 条件处程序,当满足条件sql状态码为02000是触发:关闭游标,退出存储过程declare exit handler for not found close u_cursor;drop table if exists tb_user_pro;create table if not exists tb_user_pro(id int primary key auto_increment,name varchar(100),no varchar(100));open u_cursor;while true dofetch u_cursor into uname,uno;insert into tb_user_pro values (null,uname,uno);end while;close u_cursor;
end;call p12(10);