提问人:nietoperz21 提问时间:11/19/2022 最后编辑:nietoperz21 更新时间:11/19/2022 访问量:25
PostgreSQL - 在执行过程中发生的异常是否会执行先前操作的回滚?
PostgreSQL - Does the occurrence of an exception during the execution of a procedure perform a rollback of previous operations?
问:
当调用过程时发生异常时,其中以前的操作是否会在 PostgreSQL 中回滚?在MySQL中,为了进行ROLLBACK,我必须为SQLEXCEPTION添加一个处理程序。例如:
DELIMITER //
CREATE PROCEDURE insert_address (IN region VARCHAR(25), city VARCHAR(50), street VARCHAR(50), OUT last_id INT)
BEGIN
DECLARE region_id INT;
DECLARE city_id INT;
DECLARE errno INT;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SHOW ERRORS;
ROLLBACK;
END;
START TRANSACTION;
SET region_id = (SELECT r.id FROM region AS r WHERE r.name = region LIMIT 1);
IF NOT EXISTS (SELECT c.id FROM city AS c WHERE c.name = city LIMIT 1) THEN
CALL insert_city(city, city_id);
ELSE
SET city_id = (SELECT c.id FROM city AS c LIMIT 1);
END IF;
INSERT INTO address(region_id, city_id, street)
VALUES(region_id, city_id, street);
SET last_id = LAST_INSERT_ID();
COMMIT;
END
我想知道这在 PostgreSQL 中是如何工作的,它是自动发生的还是我还必须添加一些命令。
答: 暂无答案
评论