DECLARE handler_type HANDLER FOR condition_value[,...] sp_statement
handler_type:
    CONTINUE
  | EXIT
  | UNDO
condition_value:
    SQLSTATE [VALUE] sqlstate_value
  | condition_name
  | SQLWARNING
  | NOT FOUND
  | SQLEXCEPTION
  | mysql_error_code
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.
        
              SQLWARNING is shorthand for all
              SQLSTATE codes that begin with 01.
            
              NOT FOUND is shorthand for all SQLSTATE
              codes that begin with 02.
            
              EXCEPTION is shorthand for all SQLSTATE
              codes not caught by SQLWARNING or
              NOT FOUND.
            
Além dos valores SQLSTATE, códigos de erro do MySQL 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 MySQL
          executed to the end of the procedure. If the line
          DECLARE CONTINUE HANDLER FOR '23000' SET @x2 =
          1; had not been present, MySQL 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.
        
This is a translation of the MySQL Reference Manual that can be found at dev.mysql.com. The original Reference Manual is in English, and this translation is not necessarily as up to date as the English version.

