目录
介绍
存储过程是事先经过编译并存储在数据库中的一段SQL语句的集合,调用存储过程可以简化应用开发人员的很多工作,减少数据在数据库和应用服务器之间的传输,对于提高数据处理的效率是有好处的。 存储过程思想上很简单,就是数据库SQL语言层面的代码封装与重用。
特点:
封装,复用
可以接收参数,也可以返回数据
减少网络交互,效率提升
基本语法
创建
create procedure 存储过程名称([参数列表]) begin --sql语句 end;
调用
call 名称([参数]);
查看
-- 查询指定数据库的存储过程及状态信息 select from information_schema.routines where routine_schema = 'xxx'; -- 查询某个存储过程的定义 show create procedure 存储过程名称;
删除
drop procedure [if exists] 存储过程名称;
示例:
--定义一个数据库名为‘itcast’, 其中的一个表为 ‘student’
--创建
create procedure p1()
begin
select count(*) from student
end ;
--调用
call p1();
--查看
select * from information_schema.ROUTINES where ROUTINE_SCHMA = 'itcast';
show create procedure p1 ;
--删除
drop procedure if exists p1;
单词:routine(常规,习惯) schema(图解,计划,模式) procedure(步骤,程序)
注意:在命令行中,执行创建存储过程的SQL时,需要通过关键字delimiter指定SQL语句的结束符。例如 delimiter $$
变量
系统变量
系统变量是MySQL服务器提供,不是用户定义的,属于服务器层面。分为全局变量(GLOBAL)、会话变量(SESSION)。
- 查看系统变量
--查看所有系统变量 默认为session
show [session | global] variables;
--可以通过LIKE模糊匹配方式查找变量
show [session | global] variables like '';
--查看指定变量的值g
select @@[session | global] 系统变量名 ;
设置系统变量
set [session | global] 系统变量名 = 值 ;
set @@[session | global] 系统变量名 = 值 ;
用户自定义变量
用户定义变量是用户根据需要自己定义的变量,用户变量不用提前声明,在用的时候直接用“@变量名”使用就可以。其作用域为当前 连接。
注意:用户自定义变量为一个@,系统变量为两个@,即@@
赋值
set @var_name = expr; --可以一次赋值多个
set @var_name := expr;
select @var_name := expr;
select 字段名 into @var_name from 表名;
使用
select @var_name;
示例:
set @myname = '李华' ;
set @myage := 10; --推荐使用" := "的格式,是为了区分" = "的比较运算符,因为MySQL中没有" == "赋值运算符
select @mycolor := 'red';
select count(*) into @mycount from test --将test表中的结果赋值给@mycount
select @myname,@myage;
注意: 用户定义的变量无需对其进行声明或初始化,只不过获取到的值为NULL,
局部变量
局部变量是根据需要定义的在局部生效的变量,访问之前,需要DECLARE声明。可用作存储过程内的局部变量和输入参数,局部变量 的范围是在其内声明的BEGIN...END块。
声明
declare 变量名 变量类型[default ...] ;
变量的数据类型就是数据库字段类型 : int , varchar, decimal , datetime等
赋值
set 变量名 = 值 ;
select 字段名 into 变量名 from 表名 ..;
示例:
create procedure p2()
begin
--声明
declare num int default 0 ;
--将查询结果赋值给num
select count(*) into num from text;
--查询
select num;
end;
--查看
call p2();
if判断
语法:
if 条件1 then
....
else if 条件2 then
....
else
....
end if ;
举例:
根据定义的分数score变量,判定当前分数对应的分数等级。
1.score>=85分,等级为优秀。
2.score>=60分且score<85分,等级为及格。
3.score<60分,等级为不及格
create procedure p3()
begin
declare score int default 58;
declare result varchar(10);
if score >= 85 then
set result := '优秀';
else if result >= 60 then
set result := '及格' ;
else
set result := '不及格';
end if ;
select result;
end;
参数
例子1:
根据传入参数score,判定当前分数对应的分数等级,并返回
1.score>=85分,等级为优秀。
2.score>=60分且score<85分,等级为及格。
3.score<60分,等级为不及格
create procedure p4(in score int, out result varchar(10))
begin
if score >= 85 then
set result := '优秀';
else if result >= 60 then
set result := '及格' ;
else
set result := '不及格';
end if ;
end;
call(68,@result); --第二个变量为用户自定义变量
select @result;
例子2:将传入的200分制的分数,进行换算,换算成百分制,然后返回分数
create procedure p5(inout score double)
begin
set score := score * 0.5;
end;
set @score = 78;
call p5(@score);
select @score;
case
示例:
根据传入的月份,判定月份所属的季节(要求采用case结构)。 1.1-3月份,为第一季度
2.4-6月份,为第二季度
3.7-9月份,为第三季度
4.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 := '第二季度';
when month >= 7 and 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(7);
循环while
while循环是有条件的循环控制语句。满足条件后,再执行循环体中的SQL语句。具体语法为:
示例:计算从1累加到n的值,n为传入的参数值。
-- 实现思路
-- 定义局部变量,记录累加之后的值
-- 每循环一次,会对n进行减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(10);