Domanda

I'm working a project for class where we have to make a trigger that will not allow a student to sign up for a tutoring appointment when they have 5 or more absences. It compiles with no errors, but I can't get it to fire when I insert records that would violate the constraints. If anyone could point out where I went wrong or point me in the right direction I would greatly appreciate it, I've been banging my head against the wall for a while on this.

CREATE OR REPLACE TRIGGER absence_violation
  BEFORE INSERT ON appointment FOR EACH ROW
DECLARE
  absences NUMBER(1);
BEGIN
  SELECT COUNT(app_attendance)
    INTO absences
    FROM appointment
   WHERE app_attendance = 'N'
     AND stu_id = :new.stu_id;

  IF absences >= 5 THEN
    dbms_output.put_line('ERROR 223, Student Exceeds Absence Violations');
  END IF;
END;
È stato utile?

Soluzione

Try using RAISE_APPLICATION_ERROR instead of DBMS_OUTPUT.PUT_LINE in your code.

CREATE OR REPLACE TRIGGER ABSENCE_VIOLATION
BEFORE INSERT ON APPOINTMENT
FOR EACH ROW
DECLARE Absences NUMBER (1); 
BEGIN
Select COUNT(app_attendance) INTO Absences 
FROM appointment
WHERE app_attendance = 'N' AND Stu_ID = :NEW.Stu_ID;
IF Absences >= 5 THEN RAISE_APPLICATION_ERROR(223, 'Student Exceeds Absence Violations');
END IF;
END;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top