0

I am in process of writing a simple MySQL function that looks up a value in a table and returns it. In case there is not value found it returns null. However, even with a continue handler defined I still end up with a warning "No data - zero rows fetched, selected, or processed". My code is below, what am I doing wrong? I really want to get rid of this warning :)

DROP FUNCTION IF EXISTS `getCompanySetting`;
DELIMITER |
CREATE FUNCTION `getCompanySetting`(setting_company_id INT, setting_name VARCHAR(255))
 RETURNS TEXT
BEGIN
 DECLARE setting_value TEXT DEFAULT NULL;
 DECLARE CONTINUE HANDLER FOR SQLWARNING BEGIN END;
 DECLARE CONTINUE HANDLER FOR NOT FOUND SET setting_value = NULL;
 SELECT
 value
 FROM company_settings
 WHERE
 company_id = `setting_company_id`
 AND
 name = `setting_name`
 INTO setting_value;
 RETURN setting_value;
END|
DELIMITER ;
mysql> SELECT getCompanySetting(24, 'observers_active');
+-------------------------------------------+
| getCompanySetting(24, 'observers_active') |
+-------------------------------------------+
| NULL |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> show warnings;
+---------+------+-----------------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------------+
| Warning | 1329 | No data - zero rows fetched, selected, or processed |
+---------+------+-----------------------------------------------------+
1 row in set (0.00 sec)

Update: MySQL Version 5.5.25

asked Jun 6, 2013 at 9:13

3 Answers 3

2

Before MySQL 5.6.3, if a statement that generates a warning or error causes a condition handler to be invoked, the handler may not clear the diagnostic area. This might lead to the appearance that the handler was not invoked.

I have explained this kind of behavior and the solution in this post:

Event Scheduler: No data - zero rows fetched, selected, or processed

answered Sep 5, 2013 at 5:19
0

If you ask me there is no better way to fix this kind of issue with handlers or exceptional handling declarative statements.

But you may try to give a constant value that will return even if your answer should be NULL. Let that constant value be a wierdest one that you can expect from the fucntion that could return. Add that constant value to your return function.

Disclaimer: I haven't tested it, but you may give it a try. Let me know on your results.

In this case I'm mentioning 'clops' as the constant.

Try below changes to your function only under select clause and see.

 SELECT concat('clops',value) FROM company_settings WHERE
 company_id = `setting_company_id`
 AND
 name = `setting_name`
 INTO setting_value;
mysql> SELECT getCompanySetting(24, 'observers_active');
=====>clops

As only you are aware of the word you may keep that word as a seperator. And at your application end split this word, if you get any other data after seperating 'clops' from the return type then treat it as a real return data. By this way you can ignore/suppress the warning that you get.

answered Jun 6, 2013 at 9:44
1
  • Unfortunately the warning is still there :( Commented Jun 6, 2013 at 15:57
0

One possible way around it is to generate a query which always returns a value:

SELECT
 company_settings.value
FROM (SELECT 1 as VAL) AS V
LEFT OUTER JOIN company_settings
ON company_id = `setting_company_id`
 AND
 name = `setting_name`
INTO setting_value;

You treat the response as if it were part of an outer join and the empty set is now a non-warning NULL.

Paul White
95.3k30 gold badges439 silver badges689 bronze badges
answered Jul 23, 2015 at 23:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.