I have a stored procedure that executes stored SQL.
However, the error-handler kicks-in and exits if the user attempts to execute
drop temporary table if exists t_person;
and 't_person' doesn't exist. I'm perfectly happy to generate an error when 'if exists' is not given, but how do I avoid an error for the earlier case (the error-code is unchanged)?
Here's my error handler:
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
set @sql = 'select \'Invalid SQL or bad parameter resulting in invalid SQL\' as `Error`';
prepare stmt from @sql;
execute stmt;
END;
You could use a
CONTINUEhandler rather an anEXIThandler that catches MySQL error 1051 "Unknown table"...-or-
EDIT
To catch a MySQL error in an exception handler, you need to specify the MySQL error number or the corresponding SQLSTATE to be caught. (You could specify a named condition, but that named condition has to resolve to a MySQL error number or SQLSTATE).
A syntax error would throw MySQL error 1064.
If a table
fooexists, and you issue aThat would throw MySQL error 1050.
To catch that error, declare another handler for that. Assuming you want to "swallow" the exception and continue processing...
Reference: https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html
Thelike p_personin thedrop temporary tablestatement looks wrong to me; at least, I'm not familiar with using theLIKEkeyword in aDROP TABLEstatement.