回滚后返回空表

时间:2017-12-08 13:42:01

标签: mysql sql transactions mysql-workbench rollback

我正在编写一个过程,如果没有任何回滚,我希望它返回表,如果至少有1次回滚,则返回空表/没有。

DELIMITER $$
CREATE PROCEDURE payment(IN amount int, IN profession varchar(50))
BEGIN    
DECLARE done BOOL DEFAULT FALSE;
DECLARE salary INT;
DECLARE pes VARCHAR(11);
DECLARE summary INT DEFAULT 0;

DECLARE employee_cursor CURSOR FOR (SELECT RIGHT(PESEL,3), pensja FROM Pracownicy WHERE zawod=profession);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

DROP TEMPORARY TABLE IF EXISTS tmp1;
CREATE TEMPORARY TABLE tmp1(pesel char(11), status varchar(20) DEFAULT 'wyplacono');

SET AUTOCOMMIT =0;
START TRANSACTION;
    OPEN employee_cursor;
    readLoop : LOOP
        FETCH employee_cursor INTO pes,salary;
        IF done THEN
            LEAVE readLoop;
        END IF;

        SET summary = summary + salary;

        IF( summary > amount ) THEN
            ROLLBACK;
        END IF;

        INSERT INTO tmp1(pesel) VALUES(CONCAT('********',pes));

    END LOOP;
    CLOSE employee_cursor;
    COMMIT;
    SELECT * from tmp1;    
END $$
DELIMITER ;

当它没有回滚时,它可以正常工作,但是

INSERT INTO tmp1(pesel) VALUES(CONCAT('********',pes));

似乎忽略了交易:/

1 个答案:

答案 0 :(得分:0)

将回滚测试移到循环外部。