我有一张 Giving(donor, receiver, giftname) 表格,列出了谁给谁送了礼物。我正在尝试编写一个触发器,如果它是捐赠者赠送的第一份礼物,接收者收到的第一份礼物,或者它是捐赠者和接收者的第一份礼物,它将触发。基本上我需要做的是将新捐赠者的名字与所有捐赠者的名单进行比较,并确保它还没有。然后对接收器执行相同的操作。这是我的尝试,但我确信这是不对的,因为我的变量 existingreceivers 和 existingdonors 不会做我想让他们做的事情。这些变量existingreceivers 和existingdonors 是否会存储多个值?如果没有,我该如何完成我需要做的事情?
create or replace TRIGGER FirstGift
BEFORE INSERT OR UPDATE OF donor,receiver ON GIVING
FOR EACH ROW
DECLARE
ExistingReceivers varchar(255);
ExistingDonors varchar(255);
BEGIN
select donor into existingdonors
from giving;
select receiver into existingreceivers
from giving;
--first gift for donor, first gift for receiver
if(:new.donor not in existingdonors and :new.receiver not in existingreceivers)then
dbms_output.put_line('This is the first gift received by ', :new.receiver, 'and the first given by ', :new.donor);
insert into messages (intid, donor, receiver, giftname, msg) values (test_seq.nextval, :new.donor, :new.receiver, :new.giftname, 'First Gift Given and First Received!');
--first gift for donor, not first gift for receiver
elsif(:new.donor not in existingdonors)then
dbms_output.put_line('This is the first gift given by ', :new.donor);
insert into messages (intid, donor, receiver, giftname, msg) values (test_seq.nextval, :new.donor, :new.receiver, :new.giftname, 'First Gift Given!');
--first gift for receiver, not first gift for donor
elsif(:new.receiver not in existingreceivers)then
dbms_output.put_line('This is the first gift received by ', :new.receiver);
insert into messages (intid, donor, receiver, giftname, msg) values (test_seq.nextval, :new.donor, :new.receiver, :new.giftname, 'First Gift Received!');
end if;
end;