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

Ter*_*nce 5 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
$$
Run Code Online (Sandbox Code Playgroud)

那么我怎么能用这个存储过程实现它

Ter*_*nce 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
$$
Run Code Online (Sandbox Code Playgroud)

  • 兄弟,那不是mysql (2认同)
  • 我尝试了一个mysql存储过程,我可以说它是mysql代码并且工作正常! (2认同)

小智 9

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

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 
Run Code Online (Sandbox Code Playgroud)

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