MySQL stored function, how to check for no rows and not generate a warning? -


i've function:

drop function if exists find_linkid; delimiter // create function `find_linkid`(pc1 varchar(50)     returns int begin     declare linkid int;       select a.id linkid pc_a a.pc=pc1; on              if linkid null         select b.id linkid pc_b b b.pc=pc1;     end if;      return linkid; end // 

basically, run 1 query, if doesn't return (the a.id declared not null), run query , return link id. if isn't found either, linkid null, returning null if pc1 isn't found @ ok.

this works, gives warnings if 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 - 0 rows fetched, selected, or processed | +---------+------+-----------------------------------------------------+ 1 row in set (0.00 sec) 

what's proper way of running 1 query, if doesn't return anything, run query ?

you can use continue handler catch warning, , set variable if want, or ignore giving continue handler empty body.

here's example suppress warning (i fixed missing parenthesis , removed extraneous on code):

drop function if exists find_linkid; delimiter // create function `find_linkid`(pc1 varchar(50))     returns int begin  declare linkid int;  declare continue handler not found begin end;       select a.id linkid pc_a a.pc=pc1;     if linkid null         select b.id linkid pc_b b b.pc=pc1;     end if;      return linkid; end // 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -