SP EXCEPTION不会删除它应该的数据吗?
PROCEDURE TEST (TEST IN TABLE1.COLUMN1%TYPE, TEST2 IN TABLE2.COLUMN1%TYPE)
IS
SEQ1 NUMBER;
SEQ2 NUMBER;
BEGIN
SELECT seq_id.NEXTVAL INTO SEQ1 FROM DUAL;
INSERT INTO TABLE1 VALUES(SEQ1, TEST);
SELECT seq_id2.NEXTVAL INTO SEQ2 FROM DUAL;
INSERT INTO TABLE2 VALUES(SEQ2, TEST2);
COMMIT;
EXCEPTION WHEN OTHERS THEN
ROLLBACK;
DELETE FROM TABLE1 WHERE COLUMN0 = SEQ1;
DELETE FROM TABLE2 WHERE COLUMN0 = SEQ1;
RAISE;
END;
如果出现EXCEPTION,我想删除包含该特定ID的那些表中的所有内容。这个SP被称为X次,我有3个与列有关系的表。前两个调用是OK,如果第三个调用出错,我也想删除那些先前的插入。
TABLES
CALL TO SP COLUMN1 - COLUMN2 - COLUMN3
1 1 2 3
2 1 2 3
3 1 2 ERROR
EXCEPTION
Delete from TABLES WHERE COLUMN2 = 2;
答案 0 :(得分:3)
看来,如果您将异常处理程序更改为包含
DELETE FROM TABLE1 WHERE COLUMN2 = TEST2;
DELETE FROM TABLE2 WHERE COLUMN2 = TEST2;
它会做你想要的。
但是,更好的解决方案可能是从过程中删除COMMIT和ROLLBACK,并且只有在您知道所有工作都已完成且成功时才提交。祝你好运。
答案 1 :(得分:0)
ROLLBACK已经撤消了您的更改,您能解释为什么需要其他删除语句吗?
如果您的要求是在任何(= OTHERS)出错的情况下回滚插入,这将起作用。请记住,您的序列也将被重置!
PROCEDURE TEST (TEST IN TABLE1.COLUMN1%TYPE, TEST2 IN TABLE2.COLUMN1%TYPE)
IS
BEGIN
INSERT INTO TABLE1 VALUES(seq_id.NEXTVAL, TEST);
INSERT INTO TABLE2 VALUES(seq_id2.NEXTVAL, TEST2);
COMMIT;
EXCEPTION WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;