存储过程和函数是 事先经过编译并存储在数据库中的一段 SQL 语句的集合,调用存储过程和函数可以简化应用开发人员的很多工作,减少数据在数据库和应用服务器之间的传输,对于提高数据处理的效率是有好处的。
存储过程和函数的区别在于函数必须有返回值,而存储过程没有。

  • 函数 : 是一个有返回值的过程 ;
  • 过程 : 是一个没有返回值的函数 ;

创建存储过程

  1. CREATE PROCEDURE procedure_name ([proc_parameter[,...]])
  2. begin
  3. -- SQL语句
  4. end ;

DELIMITER 该关键字用来声明SQL语句的分隔符 , 告诉 MySQL 解释器,该段命令是否已经结束了,mysql是否可以执行了。默认情况下,delimiter是分号;。在命令行客户端中,如果有一行命令以分号结束,那么回车后,mysql将会执行该命令。

示例:

  1. mysql> delimiter $
  2. mysql> create procedure pro_test1()
  3. -> begin
  4. -> select 'Hello,Mysql';
  5. -> end$
  6. Query OK, 0 rows affected (0.00 sec)
  7. mysql> delimiter ;
  8. mysql> call pro_test1();
  9. +-------------+
  10. | Hello,Mysql |
  11. +-------------+
  12. | Hello,Mysql |
  13. +-------------+

调用存储过程

  1. call procedure_name() ;

查看存储过程

  1. -- 查询db_name数据库中的所有的存储过程
  2. select name from mysql.proc where db='db_name';
  3. -- 查询存储过程的状态信息
  4. show procedure status;
  5. -- 查询某个存储过程的定义
  6. show create procedure test.pro_test1 \G;

删除存储过程

  1. DROP PROCEDURE [IF EXISTS] sp_name

语法

存储过程是可以编程的,意味着可以使用变量,表达式,控制结构 , 来完成比较复杂的功能。

变量

  • DECLARE

通过 DECLARE 可以定义一个局部变量,该变量的作用范围只能在 BEGIN…END 块中。

  1. DECLARE var_name[,...] type [DEFAULT value]

示例:

  1. mysql> delimiter $
  2. mysql> create procedure pro_test2()
  3. -> begin
  4. -> declare num int default 5;
  5. -> select num+10;
  6. -> end $
  7. Query OK, 0 rows affected (0.00 sec)
  8. mysql> call pro_test2()$
  9. +--------+
  10. | num+10 |
  11. +--------+
  12. | 15 |
  13. +--------+
  • SET

直接赋值使用 SET,可以赋常量或者赋表达式,具体语法如下:

  1. SET var_name = expr [, var_name = expr] ...

示例

  1. mysql> create procedure pro_test3()
  2. -> begin
  3. -> declare name varchar(20);
  4. -> set name='MYSQL';
  5. -> select name;
  6. -> end $
  7. Query OK, 0 rows affected (0.00 sec)
  8. mysql> call pro_test3()$
  9. +-------+
  10. | name |
  11. +-------+
  12. | MYSQL |
  13. +-------+
  14. 1 row in set (0.00 sec)

也可以通过select ... into 方式进行赋值操作 :

  1. mysql> delimiter $
  2. mysql> create procedure pro_test5()
  3. -> begin
  4. -> declare num int ;
  5. -> select count(*) into num from mysql.user;
  6. -> select num;
  7. -> end $
  8. Query OK, 0 rows affected (0.01 sec)
  9. mysql> call pro_test5()$
  10. +------+
  11. | num |
  12. +------+
  13. | 3 |
  14. +------+

if条件判断

语法结构

  1. if search_condition then statement_list
  2. [elseif search_condition then statement_list] ...
  3. [else statement_list]
  4. end if;

示例需要:

根据定义的身高变量,判定当前身高的所属的身材类型 180 及以上 —————> 身材高挑 170 - 180 ————-> 标准身材 170 以下 —————> 一般身材

  1. create procedure pro_test6()
  2. begin
  3. declare height int default 175;
  4. declare description varchar(50);
  5. if height >= 180 then
  6. set description = "身材高挑";
  7. elseif height >=170 and height <= 180 then
  8. set description = "标准身材";
  9. else
  10. set description = "一般身材";
  11. end if;
  12. select description;
  13. end $
  14. ----调用
  15. mysql> call pro_test6()$
  16. +-----------+
  17. | description |
  18. +-----------+
  19. | 标准身材 |
  20. +-----------+
  21. 1 row in set (0.16 sec)

传递参数

语法格式:

  1. create procedure procedure_name([in/out/inout] 参数名 参数类型)
  2. ...
  3. IN : 该参数可以作为输入,也就是需要调用方传入值 , 默认
  4. OUT: 该参数作为输出,也就是该参数可以作为返回值
  5. INOUT: 既可以作为输入参数,也可以作为输出参数

IN-输入
示例需求:

根据定义的身高变量,判定当前身高的所属的身材类型

  1. create procedure pro_test5(in height int)
  2. begin
  3. declare description varchar(50) default '';
  4. if height >=180 then
  5. set description ='身材高挑';
  6. elseif height >=170 and height < 180 then
  7. set description = '标准身材';
  8. else
  9. set description ='一般身材';
  10. end if;
  11. select concat('身高',height,'对应的身材类型为',description);
  12. end $
  13. ----调用情况
  14. mysql> call pro_test5(105)$
  15. +----------------------------------------------+
  16. | concat('身高',height,'对应的身材类型为',description) |
  17. +----------------------------------------------+
  18. | 身高105对应的身材类型为一般身材 |
  19. +----------------------------------------------+
  20. 1 row in set (0.17 sec)

OUT-输出
需求:

根据传入的身高变量,获取当前身高的所属的身材类型

示例:

  1. create procedure pro_test4(in height int,out description varchar(100))
  2. begin
  3. if height >=180 then
  4. set description = '身材高挑';
  5. elseif height >=170 and height <180 then
  6. set description = '身材标准';
  7. else
  8. set description = '一般身材';
  9. end if;
  10. end $

调用

  1. mysql> call pro_test4(178,@description)$
  2. Query OK, 0 rows affected (0.01 sec)
  3. mysql> select @description$
  4. +-------------+
  5. | @description |
  6. +-------------+
  7. | 身材标准 |
  8. +-------------+
  9. 1 row in set (0.11 sec)

小知识 @description : 这种变量要在变量名称前面加上“@”符号,叫做用户会话变量,代表整个会话过程他都是有作用的,这个类似于全局变量一样。 @@global.sort_buffer_size : 这种在变量前加上 “@@” 符号, 叫做 系统变量

case结构

语法结构

  1. 方式一 :
  2. CASE case_value
  3. WHEN when_value THEN statement_list
  4. [WHEN when_value THEN statement_list] ...
  5. [ELSE statement_list]
  6. END CASE;
  7. 方式二 :
  8. CASE
  9. WHEN search_condition THEN statement_list
  10. [WHEN search_condition THEN statement_list] ...
  11. [ELSE statement_list]
  12. END CASE;

需求

给定一个月份, 然后计算出所在的季度

示例:

  1. create procedure pro_test2(month int)
  2. begin
  3. declare description varchar(50);
  4. case
  5. when month >=1 and month <=3 then
  6. set description = '第一季度';
  7. when month >= 4 and month <=6 then
  8. set description = '第二季度';
  9. when month >= 7 and month <=9 then
  10. set description = '第三季度';
  11. else
  12. set description = '第四季度';
  13. end case;
  14. select concat('你输入的月份为:',month,',该月份为:',description) as content;
  15. end $

调用结果

  1. mysql> call pro_test2(1)$
  2. +--------------------------------------+
  3. | content |
  4. +--------------------------------------+
  5. | 你输入的月份为:1,该月份为:第一季度 |
  6. +--------------------------------------+
  7. 1 row in set (0.11 sec)

while循环

语法结构:

  1. while search_condition do
  2. statement_list
  3. end while;

需求:

计算从1加到n的值

  1. create procedure pro_test7(n int)
  2. begin
  3. declare total int default 0;
  4. declare num int default 1;
  5. while num <= n do
  6. set total = total + num;
  7. set num = num +1;
  8. end while;
  9. select total;
  10. end $

调用

  1. mysql> call pro_test7(100)$
  2. +------+
  3. | total |
  4. +------+
  5. | 5050 |
  6. +------+
  7. 1 row in set (0.94 sec)
  8. Query OK, 0 rows affected (0.89 sec)

repeat结构

有条件的循环控制语句, 当满足条件的时候退出循环 。while 是满足条件才执行,repeat 是满足条件就退出循环。
语法结构

  1. REPEAT
  2. statement_list
  3. UNTIL search_condition
  4. END REPEAT;

需求:

计算从1加到n的值

示例:

  1. delimiter $
  2. create procedure pro_test8(in n int)
  3. begin
  4. declare total int default 0;
  5. repeat
  6. set total = total + n;
  7. set n = n -1;
  8. until n = 0
  9. end repeat;
  10. select total;
  11. end $

调用

  1. mysql> call pro_test8(60)$
  2. +------+
  3. | total |
  4. +------+
  5. | 1830 |
  6. +------+
  7. 1 row in set (0.35 sec)
  8. Query OK, 0 rows affected (0.27 sec)

loop语句

LOOP 实现简单的循环,退出循环的条件需要使用其他的语句定义,通常可以使用 LEAVE 语句实现,具体语法如下:

  1. [begin_label:] LOOP
  2. statement_list
  3. END LOOP [end_label]

如果不在 statement_list 中增加退出循环的语句,那么 LOOP 语句可以用来实现简单的死循环。

leave语句