In one of my previous posts, CONTINUE HANDLER in MySQL – One way, with examples, I hinted at a forthcoming piece on the topic of using a CURSOR
without a CONTINUE HANDLER
. Read on and learn the exact error you should see and more importantly, what it means.

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:
If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!
The below Stored Procedure is quite similar to the Procedure demonstrated in the previous post (linked above), yet with one subtle, but important difference.
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 | DELIMITER $$ CREATE DEFINER = CURRENT_USER PROCEDURE bv_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 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; 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 ; |
Did you notice there is no CONTINUE HANDLER
in this Procedure?
What happens now if I CALL
it?
Only one way to find out…
1 2 | mysql> CALL bv_copies_each_genre('Fantasy'); ERROR 1329 (02000): No data - zero rows fetched, selected, or processed |
An explicit error message is returned that we can put to use in troubleshooting and to learn more about the condition in general. I’ll refer to some excellent online documentation for better understanding. (The MySQL official documentation lists the error message here, however, it pretty much mirrors that provided in the command-line output above.)
Basically, this error occurs because the CURSOR
is out of rows, having traversed to the end of the result set provided via the SELECT
statement it is comprised of.
The MariaDB Cursor Overview documentation surmises it very well so I encourage you to consult that resource.
Also, the Cursor FETCH section from the MySQL docs names this as a No Data condition.
I hope through this quick blog post, you now know why you must handle a CURSOR
in some way. Typically, it’s with a CONTINUE HANDLER
but in a coming-up blog post, I’ll try my hand at using an EXIT HANDLER
instead so be sure and stay tuned for that post when it drops!
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.