简体   繁体   English

Oracle触发器简单SQL

[英]Oracle Trigger Simple SQL

I am attempting to write this trigger but it is not working right. 我正在尝试编写此触发器,但无法正常工作。 I have a table GIVING(donor, receiver, giftname) and a table PERSONS(pname, age, haircolor). 我有一个表GIVING(捐赠者,接收者,giftname)和一个表PERSONS(pname,年龄,发色)。 for the trigger I want it to spit out a message whenever the donor and receiver have the same hair color. 对于触发器,我希望它在捐赠者和接收者的头发颜色相同时发出一则消息。 Trivial, I know, but I'm just starting to learn triggers and am totally stuck on why this won't work. 我知道这很简单,但是我才刚刚开始学习触发器,并且完全陷入了为什么这行不通的问题。 Thanks. 谢谢。

create or replace TRIGGER SameHairColor
BEFORE INSERT OR UPDATE OF GIFTNAME ON GIVING
DECLARE
  haircolordonor varchar(255);
  haircolorreceiver varchar(255);
BEGIN
select persons.haircolor into haircolordonor
from persons, giving
where (donor = persons.pname);
select persons.haircolor into haircolorreceiver
from persons, giving
where (receiver = persons.pname);

if (haircolordonor = haircolorreceiver) then

dbms_output.put_line('Wow, they have the same haircolor!  Who would have thought?');
end if;

end;

The error message I get is error during execution "exact fetch returns more than requested number of rows", pointing to the row below DECLARE...? 我得到的错误消息是执行期间出错,“精确获取返回的行数超过了请求的行数”,指向DECLARE下的行...?

You are not restricting your queries to the donor/receiver of the inserted/updated row. 您的查询不限于插入/更新的行的供体/接收者。 They should look like this: 它们应如下所示:

select persons.haircolor
into haircolordonor
from persons
where persons.pname = :NEW.donor;

BTW: Your trigger only fires on INSERT OR UPDATE OF GIFTNAME , while it should fire on INSERT OR UPDATE OF donor, receiver and you need to use FOR EACH ROW . 顺便说一句:您的触发器仅在INSERT OR UPDATE OF GIFTNAME上触发,而它应该在INSERT OR UPDATE OF donor, receiverINSERT OR UPDATE OF GIFTNAME ,因此您需要使用FOR EACH ROW

Your trigger should look like this: 您的触发器应如下所示:

CREATE OR REPLACE TRIGGER SameHairColor
BEFORE INSERT OR UPDATE OF donor, receiver ON GIVING
  FOR EACH ROW
DECLARE
  haircolordonor varchar(255);
  haircolorreceiver varchar(255);
BEGIN
  select persons.haircolor into haircolordonor
  from persons
  where (persons.pname = :NEW.donor);
  select persons.haircolor into haircolorreceiver
  from persons
  where (persons.pname = :NEW.receiver);

  IF (haircolordonor = haircolorreceiver) THEN
    dbms_output.put_line('Wow, they have the same haircolor!  Who would have thought?');
  END IF;
END;
CREATE OR REPLACE TRIGGER SameHairColor
BEFORE INSERT OR UPDATE OF GIFTNAME ON GIVING
REFERENCING NEW ROW AS new
FOR EACH ROW
DECLARE
  haircolordonor varchar(255);
  haircolorreceiver varchar(255);
BEGIN
select persons.haircolor into haircolordonor
from persons, giving
where (:new.donor = persons.pname);
select persons.haircolor into haircolorreceiver
from persons, giving
where (:new.receiver = persons.pname);

if (haircolordonor = haircolorreceiver) then
    dbms_output.put_line('Wow, they have the same haircolor!  Who would have thought?');
end if;

end;

Perhaps this might help? 也许这会有所帮助?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM