还是上次那个触发器的问题,不过修改后单独再发一次,把表结构也弄出来了
-------------
student      |
-------------|
studentID{PK)|
Firstname    |
Lastname     |
dateEnrolled |
dateWithdrawn|
dateGraduated|
-----------------------------
registration    |
----------------|
studentID(PK,FK)|
moduleID(PK,FK) |
year(PK)        |
semester(PK)    |
cwMark          |
ExamMark        |
---------------- TRIGGER CHECKMODULECOUNT 出现错误:LINE/COL ERROR
8/16 PLS-00049: 错误的赋值变量 'NEW.LASTNAME'
create or replace trigger checkmodulecount
before insert on registration
for each row
declare
moduleCount number;
ex exception;begin
select count(moduleid) into moduleCount
from  registration r,student s
where lastname=:new.lastname
and  studentid=:new.studentid
and moduleid = :new.moduleid
and r.studentid=s.studentid
and (r.semester='2' or r.semester='1');
if moduleCount < 4 then
dbms_output.put_line('Module registered successful');
else
raise ex;
end if;
exception
when ex then
raise_application_error(-20000, 'Student  '|| lastname || ' ID '||studentid || ' has register 4 modules already, the number of module for a particular semester should not exceed 4');
end; 

解决方案 »

  1.   

    registration表有4个主键?这是oracle吗?
      

  2.   

    LZ还真是锲而不舍啊,不过建议你先看一下trigger的基础资料。new值,old值都是针对触发表的。你的触发表是registration ,结构是
    registration    | 
    ----------------| 
    studentID(PK,FK)| 
    moduleID(PK,FK) | 
    year(PK)        | 
    semester(PK)    | 
    cwMark          | 
    ExamMark        | 
    ---------------- 
    so,:new.lastname根本不存在,肯定报错。 registration 和student 关联,仅需要studentID即可。
    select count(moduleid) into moduleCount 
    from  registration r,student s 
    where r.studentid=:new.studentid 
    and r.studentid=s.studentid 
    and (r.semester='2' or r.semester='1'); 如果想得到lastname,再去student表里读。另外,这个需求没必要使用trigger,外部判定就可以。
      

  3.   

    不好意思啊,没把整个系统的表弄出来,其实这个是一个钮代表,另外还有一个表module我没打出来,
    加上year(PK)semester(PK) ,他们是共同的约束构成这个表的主键,没问题的
      

  4.   

    这位大侠,那这个lastname怎么读出来呢?
    LINE/COL  ERROR
    18/1  PL/SQL: Statement ignored
    18/47  PLS-00201: 必须声明标识符 'S.LASTNAME' 

    create or replace trigger checkmodulecount
    before insert on registration
    for each row
    declare
    moduleCount number;
    ex exception;begin
    select count(moduleid) into moduleCount
    from  registration r,student s
    where r.studentid=:new.studentid
    and r.studentid=s.studentid
    and (r.semester='2' or r.semester='1'); 
    if moduleCount < 4 then
    dbms_output.put_line('Module registered successful');
    else
    raise ex;
    end if;
    exception
    when ex then
    raise_application_error(-20000, 'Student  '|| s.lastname|| ' ID '||studentid || ' has register 4 modules already, the number of module for a particular semester should not exceed 4');
    end; 
      

  5.   

    上面已经说了,如果想得到lastname,再去student表里读。 select lastname into tname from students where studentid=:new.studentid;
      

  6.   

    两个select怎么分隔开呢?我是新手,请教大侠指明应该怎么做?给附加分!拜托了