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') |
+------------------------------+
| 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 HANDLERto catch the warning, and then set a variable if you want, or just ignore it by giving theCONTINUE HANDLERan empty body.Here’s an example to suppress the warning (I also fixed a missing parenthesis and removed the extraneous ON from your code):