我正在尝试捕获ORA-02292 child record found
异常,以使用自己的消息引发自定义异常。
这是我的简单触发器:
CREATE OR REPLACE TRIGGER modifOuSuppressionSucc
BEFORE DELETE OR UPDATE ON Succursale
FOR EACH ROW
BEGIN
IF DELETING THEN
UPDATE Projet SET NumSuccursale = NULL
WHERE NumSuccursale = :OLD.NumSuccursale;
ELSE
IF UPDATING AND :NEW.NumSuccursale!=:OLD.NumSuccursale THEN
UPDATE Projet SET NumSuccursale = :NEW.NumSuccursale
WHERE NumSuccursale = :OLD.NumSuccursale;
END IF;
END IF;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20010, 'La modification ou la suppression d''une succursale n''est pas possible. Erreur '||SQLCODE||' -ERREUR- '||SQLERRM);
END;
当我尝试使用以下命令删除父表中的行时,Oracle引发-02292异常(这是正确的),但是我的自定义异常未通过WHEN OTHERS THEN
子句引发。
DELETE Succursale WHERE NumSuccursale=4;
我收到标准的ORA-02292 child record found
消息:
Erreur commençant à la ligne: 48 de la commande -
DELETE Succursale WHERE NumSuccursale=4
Rapport d'erreur -
ORA-02292: violation de contrainte (HB691016.SYS_C003806517) d'intégrité - enregistrement fils existant
代替我的-20010
自定义错误消息。
我需要更改使其正确吗?
答案 0 :(得分:0)
触发器不适合此操作。如果您一定要在父表中拥有不受您控制的删除,那么拥有FOREIGN KEY
并不是一个很好的设计。
您可以使用DEFERRABLE Clause作为约束。这使您可以在提交事务之前,在单个语句中运行删除和更新。
ALTER TABLE Projet ADD CONSTRAINT fk_succurscale foreign key(NumSuccursale)
references Succursale(NumSuccursale) initially deferred deferrable
以下是其工作原理的说明。
declare
l_del Succursale.NumSuccursale%TYPE := 3;
l_upd_from Succursale.NumSuccursale%TYPE := 4;
l_upd_to Succursale.NumSuccursale%TYPE := 3;
BEGIN
DELETE Succursale WHERE NumSuccursale=l_del;
UPDATE Projet SET NumSuccursale=NULL WHERE NumSuccursale=l_del;
COMMIT;--Constraint checking won't happen until this commit is issued.
UPDATE Succursale SET NumSuccursale=l_upd_to WHERE NumSuccursale=l_upd_from;
UPDATE Projet SET NumSuccursale=l_upd_to WHERE NumSuccursale=l_upd_from;
COMMIT;
END;
/
DEMO以上的块。