MySQL stored function, how to check for no rows and not generate a warning?
I've this function:
DROP FUNCTION IF EXISTS find_linkid;
DELIMITER //
CREATE FUNCTION `find_linkid`(pc1 VARCHAR(50)
RETURNS INT
BEGIN
DECLARE linkId int;
SELECT a.id INTO linkId FROM PC_A a WHERE a.pc=pc1;
ON
IF linkId IS NULL THEN
SELECT b.id INTO linkId FROM PC_B b WHERE b.pc=pc1;
END IF;
RETURN linkId;
END
//
Basically, run one query, if that doesn't return anything (the a.id is declared as NOT NULL), run another query and return the link id. If that isn't found either, linkId will be NULL, returning NULL if pc1 isn't found at all is OK.
This works, but gives warnings if the first query doesn't return anything:
select find_linkid('12BD');
+------------------------------+
| find_linkid('12BD') |
+------------------------------+
| 开发者_StackOverflow中文版 667 |
+------------------------------+
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)
What's the proper way of running one query, if that doesn't return anything, run another query ?
You can use a CONTINUE HANDLER
to catch the warning, and then set a variable if you want, or just ignore it by giving the CONTINUE HANDLER
an empty body.
Here's an example to suppress the warning (I also fixed a missing parenthesis and removed the extraneous ON from your code):
DROP FUNCTION IF EXISTS find_linkid;
DELIMITER //
CREATE FUNCTION `find_linkid`(pc1 VARCHAR(50))
RETURNS INT
BEGIN
DECLARE linkId int;
DECLARE CONTINUE HANDLER FOR NOT FOUND BEGIN END;
SELECT a.id INTO linkId FROM PC_A a WHERE a.pc=pc1;
IF linkId IS NULL THEN
SELECT b.id INTO linkId FROM PC_B b WHERE b.pc=pc1;
END IF;
RETURN linkId;
END
//
Here's another possibility with less boilerplate:
DECLARE _found, _id integer;
SELECT count(*), sum(a.id) INTO _found, _id FROM PC_A AS a WHERE a.pc = pc1;
IF _found THEN RETURN _id; END IF;
SELECT b.id INTO _id FROM PC_B AS b WHERE b.pc = pc1;
RETURN _id;
Try this handler:
DECLARE CONTINUE HANDLER FOR NOT FOUND SET linkId = NULL;
精彩评论