我正在使PL / SQL触发学生注册系统,但问题很严重

时间:2018-10-25 02:32:19

标签: oracle plsql

我正在尝试通过触发器让学生注册。我有两个表:Student和Subject

学生桌

  Stuno  Stuname
  ****** *******
   1001  James
   1002  Jacob
   1003  Misa

主题表

   Subno  Subname
   *****  *******************
     51    Computer science
     52    Statistic
     53    Engineering Electromagnetics

我创建了注册表-它为空。

注册人数表

  eno   stuno   subno
  ****  ******  *********  

我想获得预期的输出,就像

  eno   stuno   subno
  ****  ******  *********  
   1      1001    51
   2      1002    52
   3      1003    53

但是在写

时只有问题.....
 insert into Enrollments values(1,1001,51);
 insert into Enrollments values(1,1001,51)
 ERROR at line 1:
 ORA-01403: no data found
 ORA-06512: at "SCOTT.ENROLL", line 14
 ORA-04088: error during execution of trigger 'SCOTT.ENROLL'

我知道为什么会这样。由于登记表为空...无数据 当pl / sql中的Enrollments为空时,我不知道如何将数据插入表中。

这是我的编码...请帮助我.....

 drop trigger Enroll;
 create or replace trigger Enroll
 before insert or update
 on Enrollments FOR EACH ROW
 declare 
      p_snum    student.Stuno%type;
      p_ClassNum subject.subno%type; 
      p_num_current_enrolled NUMBER; 
      p_num_max_capacity NUMBER;
 BEGIN
         p_num_current_enrolled  := 0;
         p_num_max_capacity  := 0 ;

          SELECT Count(*) into p_num_current_enrolled
          from Enrollments 
          where subno = p_ClassNum;

          SELECT capacity into p_num_max_capacity
          from subject
         where subno = p_ClassNum; 

         IF p_num_current_enrolled < p_num_max_capacity THEN
             insert into Enrollments values(null,p_snum, p_ClassNum);
             dbms_output.put_line('수강 신청을 완료 하였습니다.');
          ELSE
             dbms_output.put_line('정원이 초과 하였습니다.');
           END IF;
   end;
   /

1 个答案:

答案 0 :(得分:1)

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>