Rechercher dans le manuel MySQL

13.6.7.1 DECLARE ... CONDITION Syntax

  1. DECLARE condition_name CONDITION FOR condition_value
  2.  
  3. condition_value: {
  4.     mysql_error_code
  5.   | SQLSTATE [VALUE] sqlstate_value
  6. }

The DECLARE ... CONDITION statement declares a named error condition, associating a name with a condition that needs specific handling. The name can be referred to in a subsequent DECLARE ... HANDLER statement (see Section 13.6.7.2, “DECLARE ... HANDLER Syntax”).

Condition declarations must appear before cursor or handler declarations.

The condition_value for DECLARE ... CONDITION indicates the specific condition or class of conditions to associate with the condition name. It can take the following forms:

  • mysql_error_code: An integer literal indicating a MySQL error code.

    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.

    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 names referred to in SIGNAL or use RESIGNAL statements must be associated with SQLSTATE values, not MySQL error codes.

Using names for conditions can help make stored program code clearer. For example, this handler applies to attempts to drop a nonexistent table, but that is apparent only if you know that 1051 is the MySQL error code for unknown table:

  1. DECLARE CONTINUE HANDLER FOR 1051
  2.     -- body of handler
  3.   END;

By declaring a name for the condition, the purpose of the handler is more readily seen:

  1. DECLARE no_such_table CONDITION FOR 1051;
  2. DECLARE CONTINUE HANDLER FOR no_such_table
  3.     -- body of handler
  4.   END;

Here is a named condition for the same condition, but based on the corresponding SQLSTATE value rather than the MySQL error code:

  1. DECLARE no_such_table CONDITION FOR SQLSTATE '42S02';
  2. DECLARE CONTINUE HANDLER FOR no_such_table
  3.     -- body of handler
  4.   END;

Find a PHP function

Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-declare-condition.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:en Manuel MySQL : https://dev.mysql.com/

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut