MySQL exception handler access exception being handled

半腔热情 提交于 2019-12-01 04:08:57

Looks like RESIGNAL is what you are looking for.

RESIGNAL makes it possible to both handle an error and return the error information. Otherwise, by executing an SQL statement within the handler, information that caused the handler's activation is destroyed. RESIGNAL also can make some procedures shorter if a given handler can handle part of a situation, then pass the condition “up the line” to another handler.

DELIMITER $$

DROP PROCEDURE IF EXISTS `test`.`resig` $$
CREATE PROCEDURE `test`.`resig` ()
BEGIN

DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
  SELECT 'I executed something before throwing the error' as `this_works`;
  RESIGNAL;
END;

SELECT foo FROM bar WHERE baz = 0;

END $$

DELIMITER ;


mysql> call resig();
+------------------------------------------------+
| this_works                                     |
+------------------------------------------------+
| I executed something before throwing the error |
+------------------------------------------------+
1 row in set (0.00 sec)

ERROR 1054 (42S22): Unknown column 'foo' in 'field list'

mysql>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!