drop trigger CheckMaxEnroll;

create or replace trigger CheckMaxEnroll
         before insert
         on transcripts
            referencing new as N
         for each row
DECLARE
         x number(10);
        Some_Error EXCEPTION;
BEGIN
    select maxenroll into x
    from schedule S
     where :N.crscode = S.crscode
           and :N.semester = S.semester;
     if (:N.grade = 'I' and x = 0) then
        RAISE Some_Error;
    end if;

     if (:N.grade = 'I' and x > 0) then
        update schedule set maxenroll = maxenroll - 1
         where :N.crscode = crscode
           and :N.semester = semester;
     end if;
EXCEPTION
   WHEN Some_Error THEN
        Raise_application_error(-20000, 'Error registeration over occurred');
    return;
end CheckMaxEnroll;
.
run;
