select capacity ...
вызвало ошибку;вы использовали p_ClassNum
в предложении WHERE
, а его значение неизвестно (NULL).Это должно быть :new.subno
, на самом деле.
Вот пример того, что вы можете сделать.
Сначала тестовый пример:
SQL> create table student (stuno number, stuname varchar2(20));
Table created.
SQL> insert into student
2 select 1001, 'james' from dual union all
3 select 1002, 'jacob' from dual union all
4 select 1003, 'misa' from dual;
3 rows created.
SQL> create table subject (subno number, subname varchar2(20), capacity number);
Table created.
SQL> insert into subject
2 select 51, 'compsci' , 2 from dual union all
3 select 52, 'statistic', 3 from dual;
2 rows created.
SQL> create table enrollments (eno number, stuno number, subno number);
Table created.
SQL>
Триггер:
SQL> create or replace trigger trg_Enroll_1
2 before insert on enrollments
3 for each row
4 declare
5 l_num_current_enrolled number;
6 l_capacity number;
7 begin
8 select count(*)
9 into l_num_current_enrolled
10 from enrollments
11 where subno = :new.subno;
12 select capacity
13 into l_capacity
14 from subject
15 where subno = :new.subno;
16
17 if l_num_current_enrolled >= l_capacity then
18 raise_application_error(-20000, 'No more room for that subject');
19 end if;
20 end;
21 /
Trigger created.
SQL>
Тестирование:
SQL> insert into enrollments (eno, stuno, subno)
2 values (1, 1001, 51);
1 row created.
SQL> insert into enrollments (eno, stuno, subno)
2 values (2, 1002, 51);
1 row created.
SQL> insert into enrollments (eno, stuno, subno)
2 values (3, 1003, 51);
insert into enrollments (eno, stuno, subno)
*
ERROR at line 1:
ORA-20000: No more room for that subject
ORA-06512: at "SCOTT.TRG_ENROLL_1", line 15
ORA-04088: error during execution of trigger 'SCOTT.TRG_ENROLL_1'
SQL>