mysql存储过程打印错误消息和回滚

时间:2014-10-13 10:39:44

标签: mysql stored-procedures

我正在尝试编写一个存储过程,首先打印错误消息然后回滚

我试过这个,但这个剂量不起作用

我能够回滚它,但如果出现错误,则不会打印错误消息

DELIMITER 

CREATE PROCEDURE transaction_sp ()

BEGIN

DECLARE exit handler for sqlexception
BEGIN
-- ERROR
-------------------------------------------------------------------------------------- 
select "error message '%s' and errorno '%d'"------- this part in not working
-------------------------------------------------------------------------------------- 
ROLLBACK;
END;

DECLARE exit handler for sqlwarning
BEGIN
-- WARNING
-------------------------------------------------------------------------------------- 
 select "warning message '%s' and errorno '%d'"------- this part in not working
-------------------------------------------------------------------------------------- 
ROLLBACK;
END;

START TRANSACTION;
-- ADD option 5
INSERT INTO product_option(product_id,option_id,required) VALUES(insertedProductID,5,0);
SET poid = (SELECT LAST_INSERT_ID());
INSERT INTO     product_option_value(product_option_id,product_id,option_id,option_value_id,quantity,subtract,price,pr ice_prefix,points,points_prefix,weight,weight_prefix)    VALUES(poid,insertedProductID,5,50,0,0,4.99,'+',0,'+',0,'+');

-- ADD option 12
INSERT INTO product_option(product_id,option_id,required) VALUES(insertedProductID,12,1);

-- ADD option 13
INSERT INTO product_option(product_id,option_id,required)       VALUES(insertedProductID,13,0);

COMMIT;
END
$$

那么如何使用此存储过程实现它

4 个答案:

答案 0 :(得分:12)

这对我有用

DELIMITER 

CREATE PROCEDURE transaction_sp ()

BEGIN

DECLARE exit handler for sqlexception
BEGIN
-- ERROR
-------------------------------------------------------------------------------------- 
--select "error message '%s' and errorno '%d'"------- this part in not working
-------------------------------------------------------------------------------------- 
GET DIAGNOSTICS CONDITION 1
@p1 = RETURNED_SQLSTATE, @p2 = MESSAGE_TEXT;
SELECT @p1 as RETURNED_SQLSTATE  , @p2 as MESSAGE_TEXT;
ROLLBACK;
END;

DECLARE exit handler for sqlwarning
BEGIN
-- WARNING
-------------------------------------------------------------------------------------- 
-- select "warning message '%s' and errorno '%d'"------- this part in not working
-------------------------------------------------------------------------------------- 
GET DIAGNOSTICS CONDITION 1
@p1 = RETURNED_SQLSTATE, @p2 = MESSAGE_TEXT;
SELECT @p1 as RETURNED_SQLSTATE  , @p2 as MESSAGE_TEXT;
ROLLBACK;
END;

START TRANSACTION;
-- ADD option 5
INSERT INTO product_option(product_id,option_id,required) VALUES(insertedProductID,5,0);
SET poid = (SELECT LAST_INSERT_ID());
INSERT INTO     product_option_value(product_option_id,product_id,option_id,option_value_id,quantity,subtract,price,pr ice_prefix,points,points_prefix,weight,weight_prefix)    VALUES(poid,insertedProductID,5,50,0,0,4.99,'+',0,'+',0,'+');

-- ADD option 12
INSERT INTO product_option(product_id,option_id,required) VALUES(insertedProductID,12,1);

-- ADD option 13
INSERT INTO product_option(product_id,option_id,required)       VALUES(insertedProductID,13,0);

COMMIT;
END
$$

答案 1 :(得分:0)

抱歉,我可能迟到了聚会,但是在您的查询中

BEGIN
-- ERROR
-------------------------------------------------------------------------------------- 
select "error message '%s' and errorno '%d'"------- this part in not working
-------------------------------------------------------------------------------------- 
ROLLBACK;
END;

更改为

BEGIN
select "error message '%s' and errorno '%d'"; -- use missed the semicolon
ROLLBACK;
END;

因此它实际上将显示错误消息,其列名称为“ ROLLBACK”,而不是打印并回滚事务。

答案 2 :(得分:0)

这是我将打印错误消息和回滚的方式:

CREATE PROCEDURE procedure_name() 
BEGIN
    DECLARE EXIT HANDLER FOR SQLEXCEPTION
    BEGIN
        SHOW ERRORS;  --this is the only one which you need
        ROLLBACK;   
    END; 
    START TRANSACTION;
        --query 1
        --query 2
        --query 3
    COMMIT;
END 

如果查询1、2或3将引发错误,则HANDLER将捕获SQLEXCEPTION 并且SHOW ERRORS将为我们显示错误。注意:SHOW ERRORS应该是HANDLER中的第一条语句。

答案 3 :(得分:0)

某些MySQL版本将MESSAGE_TEXT常量视为导致错误显示的列。这种方式对我在MySQL 8x社区中的工作非常完美。

DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
    GET DIAGNOSTICS CONDITION 1 @ERRNO = MYSQL_ERRNO, @MESSAGE_TEXT = MESSAGE_TEXT;
    SELECT 'ERROR' AS STATUS, CONCAT('MySQL ERROR: ', @ERRNO, ': ', @MESSAGE_TEXT) AS MESSAGE;
    ROLLBACK;
END;