continue
exit
Q:何为异常?
A:程序在执行过程中有可能出错,运行时错误叫做异常。
默认情况下,当存储过程运行出错时,过程会立即终止,并打印系统错误消息。
实验环境:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<span style="font-size: 16px;">mysql><span style="color: rgba(0, 0, 0, 1);"> use TENNIS Reading table information </span><span style="color: rgba(0, 0, 255, 1);">for</span><span style="color: rgba(0, 0, 0, 1);"> completion of table and column names You can turn off </span><span style="color: rgba(0, 0, 255, 1);">this</span> feature to <span style="color: rgba(0, 0, 255, 1);">get</span> a quicker startup with -<span style="color: rgba(0, 0, 0, 1);">A Database changed mysql</span>><span style="color: rgba(0, 0, 0, 1);"> show tables; </span>+-------------------+ | Tables_in_TENNIS | +-------------------+ | COMMITTEE_MEMBERS | | MATCHES | | PENALTIES | | PLAYERS | | TEAMS | +-------------------+ <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> sec)</span> |
因为前面的实验多数用此数据库,库表结构就不再赘述了。
例:创建过程,插入一个重复的2号球队
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<span style="font-size: 16px;">mysql><span style="color: rgba(0, 0, 0, 1);"> delimiter $$ mysql</span>><span style="color: rgba(0, 0, 0, 1);"> create procedure duplicate_teams( </span>-> <span style="color: rgba(0, 0, 255, 1);">out</span><span style="color: rgba(0, 0, 0, 1);"> p_processed smallint) </span>-><span style="color: rgba(0, 0, 0, 1);"> begin </span>-> <span style="color: rgba(0, 0, 255, 1);">set</span> p_processed=<span style="color: rgba(128, 0, 128, 1);">1</span><span style="color: rgba(0, 0, 0, 1);">; </span>-> insert into TEAMS values(<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, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">third</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">); </span>-> <span style="color: rgba(0, 0, 255, 1);">set</span> p_processed=<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, 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, 0, 1);"> call duplicate_teams(@processed); <span style="color: rgba(255, 0, 0, 1);">ERROR </span></span><span style="color: rgba(255, 0, 0, 1);">1062 (23000):</span> Duplicate entry <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">2</span><span style="color: rgba(128, 0, 0, 1);">'</span> <span style="color: rgba(0, 0, 255, 1);">for</span> key <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">PRIMARY</span><span style="color: rgba(128, 0, 0, 1);">'</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);"> @processed; </span>+------------+ | @processed | +------------+ | NULL | +------------+</span> |
解析:客户端调用存储过程,运行出错,打印错误信息,过程被终止,没有输出。
DECLARE … HANDLER语句:
通过条件的定义和处理,可以在定义过程中,针对可能遇到的问题,做出相应的处理步骤。(也就是定义一个异常处理程序,指定当过程某条语句出错时,相应的采取什么操作)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span style="font-size: 16px;"><span style="color: rgba(0, 0, 0, 1);">DECLARE handler_action HANDLER FOR condition_value [, condition_value] ... statement handler_action: CONTINUE </span>|<span style="color: rgba(0, 0, 0, 1);"> EXIT condition_value: mysql_error_code </span>|<span style="color: rgba(0, 0, 0, 1);"> SQLSTATE [VALUE] sqlstate_value </span>|<span style="color: rgba(0, 0, 0, 1);"> condition_name </span>|<span style="color: rgba(0, 0, 0, 1);"> SQLWARNING </span>|<span style="color: rgba(0, 0, 0, 1);"> NOT FOUND </span>| SQLEXCEPTION</span> |
注意:declare……handler语句必须出现在变量或条件声明的后面。
当某个错误(condition_value)发生时—>执行指定的语句(statement–记录错误信息),执行完之后再决定如何操作(handler_action)。
1、handler_action
continue:继续执行当前的程序(接着执行出错的SQL的下一条语句);
exit:当前程序终止(退出当前declare所在的begin end);
目前还不支持undo功能。
2、statement
可以是单条语句或复合语句。
3、condition_value指明handler被何种条件触发;如果条件被触发,却没有handler被声明用于处理该条件,程序的进行将取决于条件类型。
在输出参数中包含出错消息的SQLSTATE码。
例1:创建过程,插入一个重复的2号球队;continue
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 33 |
<span style="font-size: 16px;">mysql><span style="color: rgba(0, 0, 0, 1);"> DELIMITER $$ mysql</span>><span style="color: rgba(0, 0, 0, 1);"> CREATE PROCEDURE small_mistake1( </span>-> OUT error VARCHAR(<span style="color: rgba(128, 0, 128, 1);">5</span><span style="color: rgba(0, 0, 0, 1);">)) </span>-><span style="color: rgba(0, 0, 0, 1);"> BEGIN </span>-> DECLARE CONTINUE HANDLER FOR SQLSTATE <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">23000</span><span style="color: rgba(128, 0, 0, 1);">'</span> -> SET error = <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">23000</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">; <span style="color: rgba(0, 128, 0, 1);">#用来记录错误发生时的一些信息 </span></span>-> -> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> error; </span>-> SET error = <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">00000</span><span style="color: rgba(128, 0, 0, 1);">'</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);"> error; </span>-> INSERT INTO TEAMS VALUES(<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, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">third</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">); <span style="color: rgba(0, 128, 0, 1);">#会出错的语句 </span></span>-> SET error = <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">23001</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">; </span>-><span style="color: rgba(0, 0, 0, 1);"> END$$ mysql</span>><span style="color: rgba(0, 0, 0, 1);"> DELIMITER ; mysql</span>><span style="color: rgba(0, 0, 0, 1);"> call small_mistake1(@a); </span>+-------+ | error | +-------+ | NULL | +-------+ +-------+ | error | +-------+ | <span style="color: rgba(128, 0, 128, 1);">00000</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);"> @a; </span>+-------+ | @a | +-------+ | <span style="color: rgba(128, 0, 128, 1);">23001</span> | +-------+</span> |
实验示例解析:
begin end块里,定义declare……handler语句用来捕获错误(待命ing),select、set、select顺序执行,insert语句出错,SQLSTATE码23000,捕获,进行异常处理(赋值记录),结束后会继续执行出错的insert语句的下一条语句……
例2:创建过程,插入一个重复的2号球队;exit
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 33 |
<span style="font-size: 16px;">mysql><span style="color: rgba(0, 0, 0, 1);"> DELIMITER $$ mysql</span>><span style="color: rgba(0, 0, 0, 1);"> CREATE PROCEDURE small_mistake2( </span>-> OUT error VARCHAR(<span style="color: rgba(128, 0, 128, 1);">5</span><span style="color: rgba(0, 0, 0, 1);">)) </span>-><span style="color: rgba(0, 0, 0, 1);"> BEGIN </span>-> DECLARE EXIT HANDLER FOR SQLSTATE <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">23000</span><span style="color: rgba(128, 0, 0, 1);">'</span> -> SET error = <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">23000</span><span style="color: rgba(128, 0, 0, 1);">'</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);"> error; </span>-> SET error = <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">00000</span><span style="color: rgba(128, 0, 0, 1);">'</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);"> error; </span>-> INSERT INTO TEAMS VALUES(<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, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">third</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">); </span>-> SET error = <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">23001</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">; </span>-><span style="color: rgba(0, 0, 0, 1);"> END$$ mysql</span>><span style="color: rgba(0, 0, 0, 1);"> DELIMITER ; mysql</span>><span style="color: rgba(0, 0, 0, 1);"> call small_mistake2(@a); </span>+-------+ | error | +-------+ | NULL | +-------+ +-------+ | error | +-------+ | <span style="color: rgba(128, 0, 128, 1);">00000</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);"> @a; </span>+-------+ | @a | +-------+ | <span style="color: rgba(128, 0, 128, 1);">23000</span> | +-------+</span> |
与例1唯一不同的是:handler_action选择的是exit,表明在异常处理结束后不会继续执行错误语句后面的语句,直接退出begin end语句块。
可以在一个过程中定义多个异常处理程序,针对不同的错误进行不同的处理。
例1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<span style="font-size: 16px;">mysql> INSERT INTO TEAMS VALUES(<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, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">third</span><span style="color: rgba(128, 0, 0, 1);">'</span>,<span style="color: rgba(128, 0, 128, 1);">5</span><span style="color: rgba(0, 0, 0, 1);">); ERROR </span><span style="color: rgba(128, 0, 128, 1);">1136</span> (21S01): Column count doesn<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">t match value count at row 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, 0, 1);"> CREATE PROCEDURE small_mistake3( </span>-> OUT error VARCHAR(<span style="color: rgba(128, 0, 128, 1);">5</span><span style="color: rgba(0, 0, 0, 1);">)) </span>-><span style="color: rgba(0, 0, 0, 1);"> BEGIN </span>-> DECLARE CONTINUE HANDLER FOR SQLSTATE <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">23000</span><span style="color: rgba(128, 0, 0, 1);">'</span> -> SET error = <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">23000</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">; </span>-> -> DECLARE CONTINUE HANDLER FOR SQLSTATE <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">21S01</span><span style="color: rgba(128, 0, 0, 1);">'</span> -> SET error = <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">21S01</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">; </span>-> -> INSERT INTO TEAMS VALUES(<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, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">third</span><span style="color: rgba(128, 0, 0, 1);">'</span>,<span style="color: rgba(128, 0, 128, 1);">5</span><span style="color: rgba(0, 0, 0, 1);">); <span style="color: rgba(0, 128, 0, 1);"> #错误语句</span> </span>-><span style="color: rgba(0, 0, 0, 1);"> END$$ mysql</span>><span style="color: rgba(0, 0, 0, 1);"> DELIMITER ; mysql</span>><span style="color: rgba(0, 0, 0, 1);"> call small_mistake3(@error); mysql</span>> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> @error; </span>+--------+ | @error | +--------+ | 21S01 | +--------+</span> |
解析:
如果SQLSTATE码是23000,异常处理执行SET error = ‘23000’;
如果SQLSTATE码是21S01,异常处理执行SET error = ’21S01’;
当然,异常处理,可以是自定义,一般都是上述方式的错误信息记录。
例如:
1 2 3 4 5 |
<span style="font-size: 16px;">DECLARE CONTINUE HANDLER FOR <span style="color: rgba(128, 0, 128, 1);">1062</span><span style="color: rgba(0, 0, 0, 1);"> SET error </span>= <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">23000</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">; DECLARE CONTINUE HANDLER FOR </span><span style="color: rgba(128, 0, 128, 1);">1136</span><span style="color: rgba(0, 0, 0, 1);"> SET error </span>= <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">21S01</span><span style="color: rgba(128, 0, 0, 1);">'</span>;</span> |
关于错误编号和SQLSTATE码:
每个MySQL错误都有一个唯一的数字错误编号(mysql_error_code),每个错误又对应一个5字符的SQLSTATE码(ANSI SQL 采用)。
SQLSTATE码对应的处理程序:
1、SQLWARNING处理程序:以‘01’开头的所有sqlstate码与之对应;
2、NOT FOUND处理程序:以‘02’开头的所有sqlstate码与之对应;
3、SQLEXCEPTION处理程序:不以‘01’或‘02’开头的所有sqlstate码,也就是所有未被SQLWARNING或NOT FOUND捕获的SQLSTATE(常遇到的MySQL错误就是非‘01’、‘02’开头的)
注意:‘01’、‘02’开头和‘1’、‘2’开头是有区别的,是不一样的错误sqlsate码。
e.g:DECLARE CONTINUE HANDLER FOR SQLWARNING,NOT FOUND,SQLEXCEPTION
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<span style="font-size: 16px;">mysql><span style="color: rgba(0, 0, 0, 1);"> DELIMITER $$ mysql</span>><span style="color: rgba(0, 0, 0, 1);"> CREATE PROCEDURE small_mistake4( </span>-> OUT error VARCHAR(<span style="color: rgba(128, 0, 128, 1);">5</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, 0, 1);"> DECLARE CONTINUE HANDLER FOR SQLWARNING,NOT FOUND,SQLEXCEPTION </span>-> SET error = <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">xxxxx</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">; </span>-> -> INSERT INTO teams VALUES(<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, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">third</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">); </span>-><span style="color: rgba(0, 0, 0, 1);"> END$$ mysql</span>><span style="color: rgba(0, 0, 0, 1);"> DELIMITER ; mysql</span>><span style="color: rgba(0, 0, 0, 1);"> call small_mistake4(@a); mysql</span>> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> @a; </span>+-------+ | @a | +-------+ | xxxxx | +-------+</span> |
e.g:DECLARE CONTINUE HANDLER FOR SQLWARNING BEGIN END;
也就是说,当遇到SQLWARNING的问题时,进行的异常处理是begin end块,因为里面什么都没有,就类同于直接忽略。
为了提高可读性,可以给某个sqlstate代码或mysql错误代码一个名字,并且在后面的异常处理程序中使用这个名字。
1 2 3 4 5 |
<span style="font-size: 16px;"><span style="color: rgba(0, 0, 0, 1);">DECLARE condition_name CONDITION FOR condition_value condition_value: mysql_error_code </span>|SQLSTATE [VALUE] sqlstate_value</span> |
1、未命名的基本格式:
1 2 3 4 |
<span style="font-size: 16px;"><span style="color: rgba(0, 0, 0, 1);">BEGIN DECLARE CONTINUE HANDLER FOR </span><span style="color: rgba(128, 0, 128, 1);">1051</span> --<span style="color: rgba(0, 0, 0, 1);"> body of handler END;</span></span> |
2、有命名的基本格式:
1 2 3 4 5 |
<span style="font-size: 16px;"><span style="color: rgba(0, 0, 0, 1);">BEGIN DECLARE <span style="color: rgba(255, 0, 0, 1);">no_such_table</span> CONDITION FOR </span><span style="color: rgba(128, 0, 128, 1);">1051</span><span style="color: rgba(0, 0, 0, 1);">; DECLARE CONTINUE HANDLER FOR no_such_table </span>--<span style="color: rgba(0, 0, 0, 1);"> body of handler END;</span></span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<span style="font-size: 16px;">mysql><span style="color: rgba(0, 0, 0, 1);"> DELIMITER $$ mysql</span>><span style="color: rgba(0, 0, 0, 1);"> CREATE PROCEDURE small_mistake5( </span>-> OUT error VARCHAR(<span style="color: rgba(128, 0, 128, 1);">5</span><span style="color: rgba(0, 0, 0, 1);">)) </span>-><span style="color: rgba(0, 0, 0, 1);"> BEGIN </span>-> DECLARE non_unique CONDITION FOR SQLSTATE <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">23000</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">; </span>-><span style="color: rgba(0, 0, 0, 1);"> DECLARE CONTINUE HANDLER FOR non_unique </span>-><span style="color: rgba(0, 0, 0, 1);"> begin </span>-> SET error = <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">23000</span><span style="color: rgba(128, 0, 0, 1);">'</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);"> error; </span> -><span style="color: rgba(0, 0, 0, 1);"> end; </span> -> -> INSERT INTO TEAMS VALUES(<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, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">third</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">); <span style="color: rgba(0, 128, 0, 1);">#会出错语句 </span></span>-><span style="color: rgba(0, 0, 0, 1);"> END$$ mysql</span>><span style="color: rgba(0, 0, 0, 1);"> DELIMITER ; mysql</span>><span style="color: rgba(0, 0, 0, 1);"> call small_mistake5(@error); </span>+-------+ | error | +-------+ | <span style="color: rgba(128, 0, 128, 1);">23000</span> | +-------+</span> |
在嵌套块的情况下,内部块中发生异常了,首先由本块的异常处理程序来处理,如果本块没有处理,则由外部块的异常处理程序来处理。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<span style="font-size: 16px;">mysql><span style="color: rgba(0, 0, 0, 1);"> DELIMITER $$ mysql</span>><span style="color: rgba(0, 0, 0, 1);"> CREATE PROCEDURE small_mistake6() </span>-><span style="color: rgba(0, 0, 0, 1);"> BEGIN </span>-> DECLARE CONTINUE HANDLER FOR SQLSTATE <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">23000</span><span style="color: rgba(128, 0, 0, 1);">'</span> -> SET @processed = <span style="color: rgba(128, 0, 128, 1);">100</span><span style="color: rgba(0, 0, 0, 1);">; -> </span>-><span style="color: rgba(0, 0, 0, 1);"> BEGIN </span>-> DECLARE CONTINUE HANDLER FOR SQLSTATE <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">21000</span><span style="color: rgba(128, 0, 0, 1);">'</span> -> SET @processed = <span style="color: rgba(128, 0, 128, 1);">200</span><span style="color: rgba(0, 0, 0, 1);">; </span>-> INSERT INTO TEAMS VALUES(<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, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">third</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">); </span>-> <span style="color: rgba(0, 0, 255, 1);">set</span> @test=<span style="color: rgba(128, 0, 128, 1);">123321</span><span style="color: rgba(0, 0, 0, 1);">; </span>-><span style="color: rgba(0, 0, 0, 1);"> END; </span>-><span style="color: rgba(0, 0, 0, 1);"> END$$ mysql</span>><span style="color: rgba(0, 0, 0, 1);"> DELIMITER ; mysql</span>><span style="color: rgba(0, 0, 0, 1);"> call small_mistake6; mysql</span>> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> @processed,@test; </span>+------------+--------+ | @processed | @test | +------------+--------+ | <span style="color: rgba(128, 0, 128, 1);">300</span> | <span style="color: rgba(128, 0, 128, 1);">123321</span> | +------------+--------+</span> |
解析:@processed=100说明内部块里的异常传播到了外部块,交由外部块的异常处理程序进行的处理。
墙裂建议:
当有多层begin end的时候,每层都应该有自己完善的异常处理,做到:自己的异常,自己这层去处理。