MySQL exception handler access exception being handled

后端 未结 1 1092
悲&欢浪女
悲&欢浪女 2021-01-12 08:54

I\'m trying to rollback on an error, but still let the client receive the error. This might actually be impossible, unless there is a way to access the error in an exception

相关标签:
1条回答
  • 2021-01-12 09:22

    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>
    
    0 讨论(0)
提交回复
热议问题