即使触发器抛出异常,也会添加PL / SQL行

时间:2018-01-12 10:25:45

标签: sql oracle plsql triggers

当我的触发器中出现异常和异常时,该行仍会添加到表中。 这是我的触发器:

CREATE or REPLACE TRIGGER t_ships
BEFORE INSERT ON DOCKING
  FOR EACH ROW
  DECLARE
    cweight SHIPS."cargo weight"%TYPE;
    cap PIERS.capacity%TYPE;
    EX_WEIGHT EXCEPTION;
    bad_date EXCEPTION;

  BEGIN

    SELECT s."cargo weight" into cweight from SHIPS s where s.SID = :NEW.SID;
    SELECT p.capacity into cap from PIERS p where p.PID = :NEW.PID;

    IF cweight>cap THEN
    RAISE ex_weight;
    ELSIF :NEW."arrival date">:NEW."departure date" THEN RAISE bad_date;
    END IF;

    UPDATE PIERS
    set "Total ships" = "Total ships" + 1
    WHERE PID = :NEW.PID;

    EXCEPTION
      WHEN EX_WEIGHT THEN
        DBMS_OUTPUT.put_line('Weight exceeds capacity');
        RETURN;
      WHEN bad_date THEN
        DBMS_OUTPUT.put_line('Arrival date is after departure date');
        RETURN;

  END;

它会输出错误详细信息,但仍会添加该行。当我尝试使用EXIT命令时,它会编译警告并且触发器无法工作。是什么导致这种情况?

1 个答案:

答案 0 :(得分:0)

[...]
   EXCEPTION
      WHEN EX_WEIGHT THEN
        DBMS_OUTPUT.put_line('Weight exceeds capacity');
        RAISE; -- Rethrow the exception
      WHEN bad_date THEN
        DBMS_OUTPUT.put_line('Arrival date is after departure date');
        RAISE; -- Rethrow the exception

  END;