阅读目录:MySQL存储过程_创建-调用-参数
- 存储过程:SQL中的“脚本”
1.创建存储过程
2.调用存储过程
3.存储过程体
4.语句块标签
1.in:向过程里传参
2.out:过程向外传参值
3.inout:in and out
# SQL语句:先编译后执行
存储过程(Stored Procedure):
一组可编程的函数,是为了完成特定功能的SQL语句集,经编译创建并保存在数据库中,用户可通过指定存储过程的名字并给定参数(需要时)来调用执行。
优点(为什么要用存储过程?):
①将重复性很高的一些操作,封装到一个存储过程中,简化了对这些SQL的调用
②批量处理:SQL+循环,减少流量,也就是“跑批”
③统一接口,确保数据的安全
相对于oracle数据库来说,MySQL的存储过程相对功能较弱,使用较少。
一、存储过程的创建和调用
>存储过程就是具有名字的一段代码,用来完成一个特定的功能。
>创建的存储过程保存在数据库的数据字典中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<span style="font-size: 16px;"><span style="color: rgba(0, 0, 0, 1);">CREATE [DEFINER </span>= { user |<span style="color: rgba(0, 0, 0, 1);"> CURRENT_USER }] PROCEDURE sp_name ([proc_parameter[,...]]) [characteristic ...] routine_body proc_parameter: [ IN </span>| OUT |<span style="color: rgba(0, 0, 0, 1);"> INOUT ] param_name type characteristic: COMMENT </span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">string</span><span style="color: rgba(128, 0, 0, 1);">'</span> |<span style="color: rgba(0, 0, 0, 1);"> LANGUAGE SQL </span>|<span style="color: rgba(0, 0, 0, 1);"> [NOT] DETERMINISTIC </span>| { CONTAINS SQL | NO SQL | READS SQL DATA |<span style="color: rgba(0, 0, 0, 1);"> MODIFIES SQL DATA } </span>| SQL SECURITY { DEFINER |<span style="color: rgba(0, 0, 0, 1);"> INVOKER } routine_body: Valid SQL routine statement [begin_label:] BEGIN [statement_list] …… END [end_label]</span></span> |
#创建数据库,备份数据表用于示例操作
1 2 3 4 |
<span style="font-size: 16px;">mysql><span style="color: rgba(0, 0, 0, 1);"> create database db1; mysql</span>><span style="color: rgba(0, 0, 0, 1);"> use db1; mysql</span>> create table PLAYERS <span style="color: rgba(0, 0, 255, 1);">as</span> <span style="color: rgba(0, 0, 255, 1);">select</span> * <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> TENNIS.PLAYERS; mysql</span>> create table MATCHES <span style="color: rgba(0, 0, 255, 1);">as</span> <span style="color: rgba(0, 0, 255, 1);">select</span> * <span style="color: rgba(0, 0, 255, 1);">from</span> TENNIS.MATCHES;</span> |
示例:创建一个存储过程,删除给定球员参加的所有比赛
1 2 3 4 5 6 7 8 9 |
<span style="font-size: 16px;">mysql><span style="color: rgba(0, 0, 0, 1);"> delimiter $$ <span style="font-family: 'Microsoft YaHei';">#将语句的结束符号从分号;临时改为两个$$(可以是自定义)</span> mysql</span>><span style="color: rgba(0, 0, 0, 1);"> CREATE PROCEDURE delete_matches(IN p_playerno INTEGER)</span> -><span style="color: rgba(0, 0, 0, 1);"> BEGIN</span> -><span style="color: rgba(0, 0, 0, 1);"> DELETE FROM MATCHES</span> -> WHERE playerno =<span style="color: rgba(0, 0, 0, 1);"> p_playerno;</span> -><span style="color: rgba(0, 0, 0, 1);"> END$$ Query OK, </span><span style="color: rgba(128, 0, 128, 1);">0</span> rows affected (<span style="color: rgba(128, 0, 128, 1);">0.01</span><span style="color: rgba(0, 0, 0, 1);"> sec) mysql</span>> delimiter ; <span style="font-family: 'Microsoft YaHei';">#将语句的结束符号恢复为分号</span></span> |
解析:
默认情况下,存储过程和默认数据库相关联,如果想指定存储过程创建在某个特定的数据库下,那么在过程名前面加数据库名做前缀;
在定义过程时,使用DELIMITER $$ 命令将语句的结束符号从分号 ; 临时改为两个$$,使得过程体中使用的分号被直接传递到服务器,而不会被客户端(如mysql)解释。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span> * <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> MATCHES; </span>+---------+--------+----------+-----+------+ | MATCHNO | TEAMNO | PLAYERNO | WON | LOST | +---------+--------+----------+-----+------+ | <span style="color: rgba(128, 0, 128, 1);">1</span> | <span style="color: rgba(128, 0, 128, 1);">1</span> | <span style="color: rgba(128, 0, 128, 1);">6</span> | <span style="color: rgba(128, 0, 128, 1);">3</span> | <span style="color: rgba(128, 0, 128, 1);">1</span> | | <span style="color: rgba(128, 0, 128, 1);">7</span> | <span style="color: rgba(128, 0, 128, 1);">1</span> | <span style="color: rgba(128, 0, 128, 1);">57</span> | <span style="color: rgba(128, 0, 128, 1);">3</span> | <span style="color: rgba(128, 0, 128, 1);">0</span> | | <span style="color: rgba(128, 0, 128, 1);">8</span> | <span style="color: rgba(128, 0, 128, 1);">1</span> | <span style="color: rgba(128, 0, 128, 1);">8</span> | <span style="color: rgba(128, 0, 128, 1);">0</span> | <span style="color: rgba(128, 0, 128, 1);">3</span> | | <span style="color: rgba(128, 0, 128, 1);">9</span> | <span style="color: rgba(128, 0, 128, 1);">2</span> | <span style="color: rgba(128, 0, 128, 1);">27</span> | <span style="color: rgba(128, 0, 128, 1);">3</span> | <span style="color: rgba(128, 0, 128, 1);">2</span> | | <span style="color: rgba(128, 0, 128, 1);">11</span> | <span style="color: rgba(128, 0, 128, 1);">2</span> | <span style="color: rgba(128, 0, 128, 1);">112</span> | <span style="color: rgba(128, 0, 128, 1);">2</span> | <span style="color: rgba(128, 0, 128, 1);">3</span> | +---------+--------+----------+-----+------+ <span style="color: rgba(128, 0, 128, 1);">5</span> rows <span style="color: rgba(0, 0, 255, 1);">in</span> <span style="color: rgba(0, 0, 255, 1);">set</span> (<span style="color: rgba(128, 0, 128, 1);">0.00</span><span style="color: rgba(0, 0, 0, 1);"> sec) mysql</span>> call delete_matches(<span style="color: rgba(128, 0, 128, 1);">57</span><span style="color: rgba(0, 0, 0, 1);">); Query OK, </span><span style="color: rgba(128, 0, 128, 1);">1</span> row affected (<span style="color: rgba(128, 0, 128, 1);">0.03</span><span style="color: rgba(0, 0, 0, 1);"> sec) mysql</span>> <span style="color: rgba(0, 0, 255, 1);">select</span> * <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> MATCHES; </span>+---------+--------+----------+-----+------+ | MATCHNO | TEAMNO | PLAYERNO | WON | LOST | +---------+--------+----------+-----+------+ | <span style="color: rgba(128, 0, 128, 1);">1</span> | <span style="color: rgba(128, 0, 128, 1);">1</span> | <span style="color: rgba(128, 0, 128, 1);">6</span> | <span style="color: rgba(128, 0, 128, 1);">3</span> | <span style="color: rgba(128, 0, 128, 1);">1</span> | | <span style="color: rgba(128, 0, 128, 1);">8</span> | <span style="color: rgba(128, 0, 128, 1);">1</span> | <span style="color: rgba(128, 0, 128, 1);">8</span> | <span style="color: rgba(128, 0, 128, 1);">0</span> | <span style="color: rgba(128, 0, 128, 1);">3</span> | | <span style="color: rgba(128, 0, 128, 1);">9</span> | <span style="color: rgba(128, 0, 128, 1);">2</span> | <span style="color: rgba(128, 0, 128, 1);">27</span> | <span style="color: rgba(128, 0, 128, 1);">3</span> | <span style="color: rgba(128, 0, 128, 1);">2</span> | | <span style="color: rgba(128, 0, 128, 1);">11</span> | <span style="color: rgba(128, 0, 128, 1);">2</span> | <span style="color: rgba(128, 0, 128, 1);">112</span> | <span style="color: rgba(128, 0, 128, 1);">2</span> | <span style="color: rgba(128, 0, 128, 1);">3</span> | +---------+--------+----------+-----+------+ <span style="color: rgba(128, 0, 128, 1);">4</span> rows <span style="color: rgba(0, 0, 255, 1);">in</span> <span style="color: rgba(0, 0, 255, 1);">set</span> (<span style="color: rgba(128, 0, 128, 1);">0.00</span> sec)</span> |
解析:
在存储过程中设置了需要传参的变量p_playerno,调用存储过程的时候,通过传参将57赋值给p_playerno,然后进行存储过程里的SQL操作。
>存储过程体包含了在过程调用时必须执行的语句,例如:dml、ddl语句,if-then-else和while-do语句、声明变量的declare语句等
>过程体格式:以begin开始,以end结束(可嵌套)
1 2 3 4 5 6 7 |
<span style="color: rgba(0, 0, 0, 1); font-size: 16px;">BEGIN BEGIN BEGIN statements; END END END</span> |
注意:每个嵌套块及其中的每条语句,必须以分号结束,表示过程体结束的begin-end块(又叫做复合语句compound statement),则不需要分号。
1 2 3 |
<span style="color: rgba(0, 0, 0, 1); font-size: 16px;">[begin_label:] BEGIN [statement_list] END [end_label]</span> |
例如:
1 2 3 4 5 6 7 |
<span style="color: rgba(0, 0, 0, 1); font-size: 16px;">label1: BEGIN label2: BEGIN label3: BEGIN statements; END label3 ; END label2; END label1</span> |
标签有两个作用:
①增强代码的可读性
②在某些语句(例如:leave和iterate语句),需要用到标签
存储过程可以有0个或多个参数,用于存储过程的定义。
3种参数类型:
IN输入参数:表示调用者向过程传入值(传入值可以是字面量或变量)
OUT输出参数:表示过程向调用者传出值(可以返回多个值)(传出值只能是变量)
INOUT输入输出参数:既表示调用者向过程传入值,又表示过程向调用者传出值(值只能是变量)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<span style="font-size: 16px;">mysql><span style="color: rgba(0, 0, 0, 1);"> delimiter $$ mysql</span>> create procedure in_param(<span style="color: rgba(0, 0, 255, 1);">in</span> p_in <span style="color: rgba(0, 0, 255, 1);">int</span><span style="color: rgba(0, 0, 0, 1);">) </span>-><span style="color: rgba(0, 0, 0, 1);"> begin </span>-> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> p_in; </span>-> <span style="color: rgba(0, 0, 255, 1);">set</span> p_in=<span style="color: rgba(128, 0, 128, 1);">2</span><span style="color: rgba(0, 0, 0, 1);">; </span>-> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> P_in; </span>-><span style="color: rgba(0, 0, 0, 1);"> end</span><span style="color: rgba(0, 0, 0, 1);">$$ mysql</span>><span style="color: rgba(0, 0, 0, 1);"> delimiter ; mysql</span>> <span style="color: rgba(0, 0, 255, 1);">set</span> @p_in=<span style="color: rgba(128, 0, 128, 1);">1</span><span style="color: rgba(0, 0, 0, 1);">; mysql</span>><span style="color: rgba(0, 0, 0, 1);"> call in_param(@p_in); </span>+------+ | p_in | +------+ | <span style="color: rgba(128, 0, 128, 1);">1</span> | +------+ +------+ | P_in | +------+ | <span style="color: rgba(128, 0, 128, 1);">2</span> | +------+<span style="color: rgba(0, 0, 0, 1);"> mysql</span>> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> @p_in; </span>+-------+ | @p_in | +-------+ | <span style="color: rgba(128, 0, 128, 1);">1</span> | +-------+</span> |
#以上可以看出,p_in在存储过程中被修改,但并不影响@p_id的值,因为前者为局部变量、后者为全局变量。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<span style="font-size: 16px;">mysql> delimiter <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 0, 0, 1);"> mysql</span>> create procedure out_param(<span style="color: rgba(0, 0, 255, 1);">out</span> p_out <span style="color: rgba(0, 0, 255, 1);">int</span><span style="color: rgba(0, 0, 0, 1);">) </span>-><span style="color: rgba(0, 0, 0, 1);"> begin </span>-> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> p_out; </span>-> <span style="color: rgba(0, 0, 255, 1);">set</span> p_out=<span style="color: rgba(128, 0, 128, 1);">2</span><span style="color: rgba(0, 0, 0, 1);">; </span>-> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> p_out; </span>-><span style="color: rgba(0, 0, 0, 1);"> end </span>-> <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 0, 0, 1);"> mysql</span>><span style="color: rgba(0, 0, 0, 1);"> delimiter ; mysql</span>> <span style="color: rgba(0, 0, 255, 1);">set</span> @p_out=<span style="color: rgba(128, 0, 128, 1);">1</span><span style="color: rgba(0, 0, 0, 1);">; mysql</span>><span style="color: rgba(0, 0, 0, 1);"> call out_param(@p_out); </span>+-------+ | p_out | +-------+ | NULL | +-------+<span style="color: rgba(0, 0, 0, 1);"> </span></span><span style="font-size: 16px; font-family: 'Microsoft YaHei';">#因为out是向调用者输出参数,不接收输入的参数,所以存储过程里的p_out为null</span> <span style="font-size: 16px;">+-------+ | p_out | +-------+ | <span style="color: rgba(128, 0, 128, 1);">2</span> | +-------+<span style="color: rgba(0, 0, 0, 1);"> mysql</span>> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> @p_out; </span>+--------+ | @p_out | +--------+ | <span style="color: rgba(128, 0, 128, 1);">2</span> | +--------+<span style="color: rgba(0, 0, 0, 1);"> <span style="font-family: 'Microsoft YaHei';">#调用了out_param存储过程,输出参数,改变了p_out变量的值</span></span></span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<span style="font-size: 16px;">mysql><span style="color: rgba(0, 0, 0, 1);"> delimiter $$ mysql</span>> create procedure inout_param(inout p_inout <span style="color: rgba(0, 0, 255, 1);">int</span><span style="color: rgba(0, 0, 0, 1);">) </span>-><span style="color: rgba(0, 0, 0, 1);"> begin </span>-> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> p_inout; </span>-> <span style="color: rgba(0, 0, 255, 1);">set</span> p_inout=<span style="color: rgba(128, 0, 128, 1);">2</span><span style="color: rgba(0, 0, 0, 1);">; </span>-> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> p_inout; </span>-><span style="color: rgba(0, 0, 0, 1);"> end </span>-><span style="color: rgba(0, 0, 0, 1);"> $$ mysql</span>><span style="color: rgba(0, 0, 0, 1);"> delimiter ; mysql</span>> <span style="color: rgba(0, 0, 255, 1);">set</span> @p_inout=<span style="color: rgba(128, 0, 128, 1);">1</span><span style="color: rgba(0, 0, 0, 1);">; mysql</span>><span style="color: rgba(0, 0, 0, 1);"> call inout_param(@p_inout); </span>+---------+ | p_inout | +---------+ | <span style="color: rgba(128, 0, 128, 1);">1</span> | +---------+ +---------+ | p_inout | +---------+ | <span style="color: rgba(128, 0, 128, 1);">2</span> | +---------+<span style="color: rgba(0, 0, 0, 1);"> mysql</span>> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> @p_inout; </span>+----------+ | @p_inout | +----------+ | <span style="color: rgba(128, 0, 128, 1);">2</span> | +----------+</span> |
#调用了inout_param存储过程,接受了输入的参数,也输出参数,改变了变量
注意:
①如果过程没有参数,也必须在过程名后面写上小括号
例:CREATE PROCEDURE sp_name ([proc_parameter[,…]]) ……
②确保参数的名字不等于列的名字,否则在过程体中,参数名被当做列名来处理
墙裂建议:
>输入值使用in参数;
>返回值使用out参数;
>inout参数就尽量的少用。