1.1 存储过程和函数概述
存储过程和函数是 事先经过编译并存储在数据库中的一段 SQL 语句的集合,调用存储过程和函数可以简化应用开发人员的很多工作,减少数据在数据库和应用服务器之间的传输,对于提高数据处理的效率是有好处的。
存储过程和函数的区别在于函数必须有返回值,而存储过程没有。
函数 : 是一个有返回值的过程;
过程 : 是一个没有返回值的函数;
1.2 创建存储过程
create procedure procedure_name ([proc_parameter[,...]])
begin
-- sql语句
end ;
示例 :
create procedure pro_test1()
begin
select 'Hello Mysql';
end ;
1.3 调用存储过程
call procedure_name() ;
1.4 查看存储过程
-- 查询db_name数据库中的所有的存储过程
select name from mysql.proc where db='db_name';
-- 查询存储过程的状态信息
show procedure status;
-- 查询某个存储过程的定义
show create procedure test.pro_test1 G;
1.5 删除存储过程
DROP PROCEDURE [1 IF EXISTS] sp_name ;
1.6 语法
存储过程是可以编程的,意外着可以使用变量,表达式,控制结构,来完成比较复杂的功能
1.6.1 变量
- declare
通过 declare 可以定义一个局部变量,改变量的作用范围只能在BEGIN.....END块中。
declare var_name[.....] type [DEFAULT value]
示例 :
create procdeure pro_test2()
begin
declare num int default 5;
select num + 10;
end;
- set
直接赋值使用SET, 可以赋值常量或者赋表达式,具体语法如下:
set var_name = expr[,var_name = expr]....
示例:
create procedure pro_test3()
begin
declare name varchar(20);
set name = 'mysql';
select name;
end;
调用 call 函数
call pro_test3();
也可以通过select ... into 方式进行赋值操作 :
create procedure pro_test4()
begin
declare countnum int;
select count(*) into countnum from city c;
select countnum;
end;
1.6.2 if条件判断
语法结构
if search_condition then statement_list
[elseif search_condition then statement_list] ...
[else statement_list]
end if;
需求:
根据定义的身高变量,判定当前身高的所属的身材类型
180 及以上 ----------> 身材高挑
170 - 180 ---------> 标准身材
170 以下 ----------> 一般身材
示例 :
create procedure pro_test5()
begin
declare height int default 175;
declare description varchar(50);
if height >= 180 then
set description = '身材高挑';
elseif height >= 170 and height < 180 then
set description = '标准身材';
else
set description = '一般身材';
end if;
select description;
end;
1.6.3 传递参数
语法格式:
create procdeure name([]in/out/inout) ) 参数名 参数类型
IN: 该参数可以作为输入,也就是需要调用方传入值,默认
OUT: 该参数作为输出,也就是该参数可以作为返回值
INOUT: 既可以作为输入参数,也可以作为输出参数
IN - 输入
需求 :
根据定义的身高变量,判定当前身高的所属的身材类型
示例 :
create procedure pro_test6(in height int)
begin
declare description varchar(50);
if height >= 180 then
set description = '身材高挑';
elseif height >= 170 and height < 180 then
set description = '标准身材';
else
set description = '一般身材';
end if;
select description;
end;
OUT-输出
需求: 根据传入的身高变量,获取当前身高的所属的身材类型
create procedure pro_test7(in height int, out description varchar(100))
begin
if height >= 180 then
set description = '身材高挑';
elseif height >= 170 and height < 180 then
set description = '标准身材';
else
set description = '一般身材';
end if;
end;
call pro_test7(158, @description);
select @description;
小知识
@description:这种变量名称起那么加上“@” 符号,叫做用户会话变量,代表整个会话过程他都是有作用的,这个类似于全局变量一样。
@@global.sort_buffer_size : 这种在变量前加上 "@@" 符号, 叫做 系统变量
1.6.4 case结构
语法结构 :
方式一 :
case case_value
when when_value then statement_list
[when when_value then statement_list] ...
[else statement_list]
end case;
方式二 :
case
when search_condition then statement_list
[when search_condition then statement_list] ...
[else statement_list]
end case;
需求:
给定一个月份, 然后计算出所在的季度
create procedure pro_test8(month int)
begin
declare result varchar(20);
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 = '第四季度';
end case;
select concat('您输入的月份为: ',month,', 该月份为 : ', result) as content ;
end;
call pro_test8(11);
1.6.5 while循环
语法结构:
while search_condition do
statement_list
end while;
需求:
计算从1加到n的值
create procedure pro_test9(in n int)
begin
declare total int default 0;
declare num int default 1;
while num <= n do
set total = total + num;
set num = num + 1;
end while;
select total;
end;
call pro_test9(11);