用于比较行的Oracle Trigger

时间:2014-05-08 19:02:15

标签: oracle plsql triggers

我有以下两个表:

persons(Pname, SocStatus, Age)
donates(donor, receiver, dateofgoft)

我需要在Donates上进行触发(插入后),比较捐赠者和接收者是否具有相同的年龄,然后打印出来。我写了一个触发器,但在声明变量时出现错误ORA00942。你能来看看吗?

create trigger STEP5
after insert on donates2
for each row
declare donage number(3);
recage number(3);
ndonor varchar(30);
nreceiver varchar(30);
begin
    select donor into ndonor from new.donor;
    select receiver into nreceiver from new.receiver;
    select distinct B.Age into donage
        from donates2 A
        inner join persons B on B.pname = A.donor
        where Pname = ndonor;
    select distinct C.Age into recage
        from donates2 A
        inner join persons C on C.pname = A.receiver
        where Pname = nreceiver;
    if (donage =  recage) then 
        dbms_output.put_line( ndonor|| ' and ' ||  nreceiver || ' is peers');
    end if;
end;

1 个答案:

答案 0 :(得分:2)

试试这个:

create trigger STEP5
  after insert on donates2
  for each row
declare
  donage number;
  recage number;
begin
    SELECT b.AGE
      INTO DONAGE
      FROM PERSONS b
      WHERE b.PNAME = :NEW.DONOR;

    SELECT c.AGE
      INTO RECAGE
      FROM PERSONS c
      WHERE c.PNAME = :NEW.RECEIVER;

    if donage = recage then 
        dbms_output.put_line(:NEW.DONOR|| ' and ' || :NEW.RECEIVER || ' ARE peers');
    end if;
end STEPS;

祝你好运。

分享并享受。