CONTINUE HANDLER in MySQL – One way, with examples.

In this blog post, we will visit an alternate CONTINUE HANDLER declaration I recently learned about while studying CURSOR‘s in MySQL.

Photo by Liam Briese on Unsplash

Note: All data, names or naming found within the database presented in this post, are strictly used for practice, learning, instruction, and testing purposes. It by no means depicts actual data belonging to or being used by any party or organization.

OS and DB used:


  • Xubuntu Linux 16.04.5 LTS (Xenial Xerus)
  • MySQL 8.0.15
Self Promotion

In the past, I have written a couple of blog posts about CURSOR‘s, and using them in Stored Procedures in MySQL. Feel free to visit the below posts for more information:

Why do you need a CONTINUE HANDLER when working with CURSOR‘s in MySQL? That question will be addressed in a forthcoming blog post soon.

But in this particular post, I want to share 1 of 2 ways (that I know of) to DECLARE, and use, a CONTINUE HANDLER with a CURSOR. There are likely more I am unaware of, so if you have other ideas and methods, I would love to know of them in the comments below. Many thanks!!!

Although I mentioned 2, just the one is covered here, since the posts I have written previously, make use of the other method (termed loosely) exclusively. Again, visit the posts linked above and see it in action.

Take a look at these 2 declarations:

1
2
DECLARE out_of_rows CONDITION FOR SQLSTATE '02000';
DECLARE CONTINUE HANDLER FOR out_of_rows SET v_finished = 1;

We have an out_of_rows CONDITION with a SQLSTATE '02000' value and a CONTINUE HANDLER that takes some action should that CONDITION happen.

Let’s see an example Stored Procedure’s definition using a CURSOR that returns a count of book titles from a specific, genre. Yet, this version will use the out_of_rows condition.

But first, we need to understand something super-important (that I often routinely must revisit – including for this post!) concerning the declaration section. If we try it like this:

1
2
3
4
5
6
    DECLARE v_finish INTEGER DEFAULT 0;
    DECLARE v_book_name VARCHAR(35);
    DECLARE v_num_copies INTEGER;
    DECLARE out_of_rows CONDITION FOR SQLSTATE '02000';
    DECLARE CONTINUE HANDLER FOR out_of_rows SET v_finish = 1;
    DECLARE v_cur CURSOR FOR SELECT b.title FROM book AS b INNER JOIN book_genre AS bg USING(genre_id) WHERE bg.genre_type = p_category;

This error is returned:

1
ERROR 1338 (42000): Cursor declaration after handler declaration

That CONTINUE HANDLER declaration must be after the CURSOR declaration.

Here is the Stored Procedure with that correction:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
DELIMITER $$
CREATE
    DEFINER = CURRENT_USER
PROCEDURE copies_each_genre(p_category VARCHAR(35))
BEGIN
    DECLARE v_finish INTEGER DEFAULT 0;
    DECLARE v_book_name VARCHAR(35);
    DECLARE v_num_copies INTEGER;
    DECLARE out_of_rows CONDITION FOR SQLSTATE '02000';
    DECLARE v_cur CURSOR FOR SELECT b.title FROM book AS b INNER JOIN book_genre AS bg USING(genre_id) WHERE bg.genre_type = p_category;
    DECLARE CONTINUE HANDLER FOR out_of_rows SET v_finish = 1;
    IF p_category NOT IN (SELECT genre_type FROM book_genre) THEN
    SELECT 'Genre choice not available.' AS final_message;
    ELSE
        OPEN v_cur;
        how_many : LOOP
            FETCH v_cur INTO v_book_name;
            IF v_finish = 1 THEN
            LEAVE how_many;
            END IF;
        END LOOP;
        CLOSE v_cur;
        SET v_num_copies = FOUND_ROWS();
        IF v_num_copies > 1 THEN
        SELECT CONCAT_WS(' ', 'There are', v_num_copies , 'titles available to choose from in the', p_category, 'genre.') AS final_message;
        ELSE
        SELECT CONCAT_WS(' ', 'There is', v_num_copies , 'title available to choose from in the', p_category, 'genre.') AS final_message;
        END IF;
    END IF;
END $$
DELIMITER ;

I’ll CALL procedure copies_each_genre with a search in the ‘Fantasy’ genre:

1
2
3
4
5
6
7
8
9
mysql> CALL copies_each_genre('Fantasy');
+-------------------------------------------------------------------+
| final_message                                                     |
+-------------------------------------------------------------------+
| There are 4 titles available to choose from in the Fantasy genre. |
+-------------------------------------------------------------------+
1 row in set (0.17 sec)

Query OK, 0 rows affected (0.17 sec)

You can see this form of CONTINUE HANDLER works as well. Like I mentioned previously in the posts, be looking for a blog post coming here on DOP for what happens when you do not use a CONTINUE HANDLER with a CURSOR.

Like what you have read? See anything incorrect? Please comment below and thanks for reading!!!

Explore the official MySQL 8.0 Online Manual for more information.

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.

Have I mentioned how much I love a cup of coffee?!?!

To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the ‘Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.


Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, is performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

Advertisements

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.