简体   繁体   English

存储过程中的EXCEPTION处理不正确。

[英]EXCEPTION from stored procedure is not handling correctly.

The SP EXCEPTION is not deleting the data that it should? 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;

If an EXCEPTION occurrs, I want to delete everything from those tables that contains that certain ID. 如果发生异常,我想从那些包含特定ID的表中删除所有内容。 This SP is called X times, and I have 3 tables that have relation with a column. 这个SP被称为X次,我有3个与一列有关系的表。 The first two calls are OK, if the third gives an error I want to delete those previous inserts too. 前两个调用正常,如果第三个调用出错,我也要删除这些先前的插入。

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; 

It appears that if you change your exception handler to include 看来,如果您将异常处理程序更改为包括

DELETE FROM TABLE1 WHERE COLUMN2 = TEST2;
DELETE FROM TABLE2 WHERE COLUMN2 = TEST2;

it'll do what you're looking for. 它会做您想要的。

A better solution, though, might be to remove the COMMIT and ROLLBACK from the procedure, and only commit once you know that all your work is done and successful. 但是,更好的解决方案可能是从过程中删除COMMIT和ROLLBACK,并且仅在知道所有工作都已成功完成后才提交。 Best of luck. 祝你好运。

The ROLLBACK will already undo your changes, can you explain why you want the additional delete statements? ROLLBACK将已经撤消您的更改,您能解释一下为什么要使用其他删除语句吗?

This will work if your requirements is to rollback the inserts in case anything (=OTHERS) goes wrong. 如果您的要求是回滚插入内容,以防万一(= OTHERS)出现问题,这将起作用。 Keep in mind that your sequence will get reset as well! 请记住,您的顺序也会被重置!

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM