MySQL - DECLARE ... HANDLER Statement

While working with stored procedures in MySQL if an exception or occurs the execution of the procedure terminates abruptly, to avoid this you need to handle the exceptions in MYSQL.
MySQL provides a handler to handle the exceptions in the stored procedures. You can handle these exceptions by declaring a handler using the MySQL DECLARE ... HANDLER Statement.
Following is the syntax of the MySQL HANDLER Statement −
DECLARE handler_action HANDLER
   FOR condition_value
   statement

The handler_action is the action to be performed when the given condition(s) are satisfied. You can provide the following as values for handler actions.
CONTINUE − The current program will continue execution of the procedure.
CONTINUE − The current program will continue execution of the procedure.
EXIT − This terminates the execution of the procedure.
EXIT − This terminates the execution of the procedure.
UNDO − InnoDB does not support this action.
UNDO − InnoDB does not support this action.
The condition_value is the condition to be satisfied, you can pass multiple condition values. You can provide the following as values for condition value.
mysql_error_code − This is an integer literal indicating the error code.
mysql_error_code − This is an integer literal indicating the error code.
sqlstate_value − This is a 5-character string literal specifying the SQLSTATE value.
sqlstate_value − This is a 5-character string literal specifying the SQLSTATE value.
condition_name − The name of the user defined condition specified with DECLARE ... CONDITION.
condition_name − The name of the user defined condition specified with DECLARE ... CONDITION.
SQLWARNING − Shorthand value for SQLSTATE starts with '01'.
SQLWARNING − Shorthand value for SQLSTATE starts with '01'.
NOT FOUND − Shorthand value for SQLSTATE starts with '02'.
NOT FOUND − Shorthand value for SQLSTATE starts with '02'.
SQLEXCEPTION − Short hand value to specify the exception.
SQLEXCEPTION − Short hand value to specify the exception.
Assume we have created a table with name tutorials in MySQL database using CREATE statement as shown below −
CREATE TABLE tutorials (
   ID INT PRIMARY KEY,
   TITLE VARCHAR(100),
   AUTHOR VARCHAR(40),
   DATE VARCHAR(40)
);
Now, we will insert 5 records in tutorials table using INSERT statements −
insert into tutorials values(1, 'Java', 'Krishna', '2019-09-01');
insert into tutorials values(2, 'JFreeCharts', 'Satish', '2019-05-01');
insert into tutorials values(3, 'JavaSprings', 'Amit', '2019-05-01');
insert into tutorials values(4, 'Android', 'Ram', '2019-03-01');
insert into tutorials values(5, 'Cassandra', 'Pruthvi', '2019-04-06');
Let us create another table to back up the data −
CREATE TABLE backup (
   ID INT,
   TITLE VARCHAR(100),
   AUTHOR VARCHAR(40),
   DATE VARCHAR(40)
);
Following is procedure demonstrates the usage of the HANDLER statement, it backups the contents of the tutorials table to the backup table using cursors −
DELIMITER //
CREATE PROCEDURE ExampleProc()
   BEGIN
      DECLARE done INT DEFAULT 0;
      DECLARE tutorialID INTEGER;
      DECLARE tutorialTitle, tutorialAuthor, tutorialDate VARCHAR(20);
      DECLARE cur CURSOR FOR SELECT * FROM tutorials;
      DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
      OPEN cur;
      label: LOOP
         FETCH cur INTO tutorialID, tutorialTitle, tutorialAuthor, tutorialDate;
         INSERT INTO backup VALUES(tutorialID, tutorialTitle, tutorialAuthor, tutorialDate);
         IF done = 1 THEN LEAVE label;
         END IF;
      END LOOP;
      CLOSE cur;
   END//
DELIMITER ;
You can call the above procedure as shown below −
mysql> CALL ExampleProc;
Query OK, 1 row affected (0.78 sec)
If you verify the contents of the backup table you can see the inserted records as shown below −
mysql> select * from backup;
+------+-------------+---------+------------+
| ID   | TITLE       | AUTHOR  | DATE       |
+------+-------------+---------+------------+
| 1    | Java        | Krishna | 2019-09-01 |
| 2    | JFreeCharts | Satish  | 2019-05-01 |
| 3    | JavaSprings | Amit    | 2019-05-01 |
| 4    | Android     | Ram     | 2019-03-01 |
| 5    | Cassandra   | Pruthvi | 2019-04-06 |
+------+-------------+---------+------------+
5 rows in set (0.08 sec)

    31 Lectures 
     6 hours 

 Eduonix Learning Solutions

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    6 Lectures 
     3.5 hours 

 DATAhill Solutions Srinivas Reddy

    60 Lectures 
     10 hours 

 Vijay Kumar Parvatha Reddy

    10 Lectures 
     1 hours 

 Harshit Srivastava

    25 Lectures 
     4 hours 

 Trevoir Williams
 Print
 Add Notes
 Bookmark this page