2

当我尝试删除不存在的行时,为什么这段代码没有捕捉到错误?无论我作为行的名称传入什么参数,它总是返回“1 行已删除”并且不使用退出处理程序。应该只捕获这种类型的错误。

USE yoga;

DROP PROCEDURE IF EXISTS delete_warmup;

DELIMITER //

CREATE PROCEDURE delete_warmup 
(
    warmup_name_param               VARCHAR(100)
)
BEGIN
DECLARE row_not_found       TINYINT DEFAULT FALSE;
DECLARE sql_exception       TINYINT DEFAULT FALSE;

BEGIN
    DECLARE EXIT HANDLER FOR 1329
        SET row_not_found = TRUE;
    DECLARE EXIT HANDLER FOR SQLEXCEPTION
        SET sql_exception = TRUE;

    DELETE FROM warmup
    WHERE warmup_name = warmup_name_param;

    SELECT '1 row was deleted.' AS message;
END;

IF row_not_found = TRUE THEN
    SELECT 'Row not deleted - row not found' AS message;
ELSEIF sql_exception = TRUE THEN
    SHOW ERRORS;
END IF;

END//

DELIMITER ;

CALL delete_warmup ('Monkey business');
4

1 回答 1

-1

您正在为重复参数使用退出处理程序:http ://www.briandunning.com/errors/596,即您指定的 1329

也许您应该尝试错误代码 1011:http ://www.briandunning.com/errors/278

另外,尝试寻找NOT FOUND以及SQLException

此外,尝试将退出处理程序放在开始/结束子句之外。

所以你的BEGINandEND子句是

DECLARE EXIT HANDLER FOR 1011
DECLARE EXIT HANDLER FOR SQLEXCEPTION, NOT FOUND
BEGIN
        SET row_not_found = TRUE;
        SET sql_exception = TRUE;

    DELETE FROM warmup
    WHERE warmup_name = warmup_name_param;

    SELECT '1 row was deleted.' AS message; 
END;
于 2013-03-25T03:14:10.033 回答