Rechercher dans le manuel MySQL
13.6.7.2 DECLARE ... HANDLER Syntax
- FOR condition_value [, condition_value] ...
- statement
- handler_action: {
- CONTINUE
- | EXIT
- | UNDO
- }
- condition_value: {
- mysql_error_code
- | condition_name
- | SQLWARNING
- | NOT FOUND
- | SQLEXCEPTION
- }
The DECLARE ...
HANDLER
statement specifies a handler that deals with
one or more conditions. If one of these conditions occurs, the
specified statement
executes.
statement
can be a simple statement
such as SET
, or a compound
statement written using var_name
=
value
BEGIN
and
END
(see Section 13.6.1, “BEGIN ... END Compound-Statement Syntax”).
Handler declarations must appear after variable or condition declarations.
The handler_action
value indicates
what action the handler takes after execution of the handler
statement:
CONTINUE
: Execution of the current program continues.EXIT
: Execution terminates for theBEGIN ... END
compound statement in which the handler is declared. This is true even if the condition occurs in an inner block.UNDO
: Not supported.
The condition_value
for
DECLARE ...
HANDLER
indicates the specific condition or class of
conditions that activates the handler. It can take the following
forms:
mysql_error_code
: An integer literal indicating a MySQL error code, such as 1051 to specify “unknown table”:Do not use MySQL error code 0 because that indicates success rather than an error condition. For a list of MySQL error codes, see Section B.3.1, “Server Error Message Reference”.
SQLSTATE [VALUE]
sqlstate_value
: A 5-character string literal indicating an SQLSTATE value, such as'42S01'
to specify “unknown table”:Do not use SQLSTATE values that begin with
'00'
because those indicate success rather than an error condition. For a list of SQLSTATE values, see Section B.3.1, “Server Error Message Reference”.condition_name
: A condition name previously specified withDECLARE ... CONDITION
. A condition name can be associated with a MySQL error code or SQLSTATE value. See Section 13.6.7.1, “DECLARE ... CONDITION Syntax”.SQLWARNING
: Shorthand for the class of SQLSTATE values that begin with'01'
.NOT FOUND
: Shorthand for the class of SQLSTATE values that begin with'02'
. This is relevant within the context of cursors and is used to control what happens when a cursor reaches the end of a data set. If no more rows are available, a No Data condition occurs with SQLSTATE value'02000'
. To detect this condition, you can set up a handler for it or for aNOT FOUND
condition.For another example, see Section 13.6.6, “Cursors”. The
NOT FOUND
condition also occurs forSELECT ... INTO
statements that retrieve no rows.var_list
SQLEXCEPTION
: Shorthand for the class of SQLSTATE values that do not begin with'00'
,'01'
, or'02'
.
For information about how the server chooses handlers when a condition occurs, see Section 13.6.7.6, “Scope Rules for Handlers”.
If a condition occurs for which no handler has been declared, the action taken depends on the condition class:
For
SQLEXCEPTION
conditions, the stored program terminates at the statement that raised the condition, as if there were anEXIT
handler. If the program was called by another stored program, the calling program handles the condition using the handler selection rules applied to its own handlers.For
SQLWARNING
conditions, the program continues executing, as if there were aCONTINUE
handler.For
NOT FOUND
conditions, if the condition was raised normally, the action isCONTINUE
. If it was raised bySIGNAL
orRESIGNAL
, the action isEXIT
.
The following example uses a handler for SQLSTATE
'23000'
, which occurs for a duplicate-key error:
- Query OK, 0 rows affected (0.00 sec)
- mysql> delimiter //
- END;
- //
- Query OK, 0 rows affected (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
- +------+
- +------+
- | 3 |
- +------+
Notice that @x
is 3
after
the procedure executes, which shows that execution continued to
the end of the procedure after the error occurred. If the
DECLARE ...
HANDLER
statement had not been present, MySQL would
have taken the default action (EXIT
) after
the second INSERT
failed due to
the PRIMARY KEY
constraint, and
SELECT @x
would have returned
2
.
To ignore a condition, declare a CONTINUE
handler for it and associate it with an empty block. For
example:
The scope of a block label does not include the code for
handlers declared within the block. Therefore, the statement
associated with a handler cannot use
ITERATE
or
LEAVE
to refer to labels for
blocks that enclose the handler declaration. Consider the
following example, where the
REPEAT
block has a label of
retry
:
The retry
label is in scope for the
IF
statement within the block. It
is not in scope for the CONTINUE
handler, so
the reference there is invalid and results in an error:
ERROR 1308 (42000): LEAVE with no matching label: retry
To avoid references to outer labels in handlers, use one of these strategies:
To leave the block, use an
EXIT
handler. If no block cleanup is required, theBEGIN ... END
handler body can be empty:Otherwise, put the cleanup statements in the handler body:
To continue execution, set a status variable in a
CONTINUE
handler that can be checked in the enclosing block to determine whether the handler was invoked. The following example uses the variabledone
for this purpose:
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 26/06/2006 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/mysql-rf-declare-handler.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.