DECLARE Handlers


Esta instrução especifica handlers para lidar com uma ou mais condições. Se uma dessas condições ocorrer, a instrução especificada é executada.

Para um handler CONTINUE, a execução das rotinas atuais continuam depois da instrução handler. Para um handler EXIT, a execução da rotina atual é terminada. O handler_type UNDO ainda não é suportado. Atualmente o UNDO se comporta como CONTINUE.

Além dos valores SQLSTATE, códigos de erro do MariaDB também são suportados.

Por exemplo:

mysql> CREATE TABLE test.t (s1 int,primary key (s1));
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter |
ysql> CREATE PROCEDURE handlerdemo ()
 -> BEGIN
 -> DECLARE CONTINUE HANDLER FOR '23000' SET @x2 = 1;
 -> set @x = 1;
 -> INSERT INTO test.t VALUES (1);
 -> set @x = 2;
 -> INSERT INTO test.t VALUES (1);
 -> SET @x = 3;
 -> END;
 -> |
Query OK, 0 rows affected (0.00 sec)
mysql> CALL handlerdemo()|
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @x|
 +------+
 | @x |
 +------+
 | 3 |
 +------+
 1 row in set (0.00 sec)

Notice that @x is 3, which shows that MariaDB executed to the end of the procedure. If the line DECLARE CONTINUE HANDLER FOR '23000' SET @x2 = 1; had not been present, MariaDB would have taken the default (EXIT) path after the second INSERT failed due to the PRIMARY KEY constraint, and SELECT @x would have returned 2.

Retornar