Condition and Handler in action : Condition HANDLER « Procedure Function « MySQL Tutorial






By declaring conditions and handlers, you can catch certain MySQL errors or SQLSTATE conditions.

mysql>
mysql>
mysql> DELIMITER //
mysql>
mysql> CREATE FUNCTION perform_logic (some_input INT(10)) returns INT(10)
    -> BEGIN
    ->         DECLARE problem CONDITION FOR 1265;
    ->         DECLARE EXIT HANDLER FOR problem
    ->                 RETURN NULL;
    ->
    ->         # do some logic, if the problem condition is met
    ->         # the function will exit, returning a NULL
    ->
    ->         RETURN 1;
    -> END
    -> //
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;
mysql>
mysql> drop function perform_logic;
Query OK, 0 rows affected (0.00 sec)

mysql>








11.44.Condition HANDLER
11.44.1.Declaring a Condition and Handler
11.44.2.Condition and Handler in action
11.44.3.DECLARE CONTINUE HANDLER FOR NOT FOUND SET
11.44.4.Checking flag
11.44.5.Using while to check the condition
11.44.6.DECLARE EXIT HANDLER FOR
11.44.7.Checking Exit flag
11.44.8.Using the IF statement to verify 'CONTINUE HANDLER'
11.44.9.The example associates a handler with SQLSTATE 23000, which occurs for a duplicate-key error
11.44.10.To ignore a condition