在oracle中使用编译错误创建的触发器

时间:2017-10-22 05:58:09

标签: sql oracle

我正在学习ORACLE并且在尝试创建此触发器时无法弄清楚为什么会出现编译错误。谢谢你的帮助!

CREATE OR REPLACE TRIGGER TR_HISTORY 
BEFORE INSERT ON HISTORY
FOR EACH ROW
DECLARE name_d varchar2(50), breed_d varchar2(50), area_d varchar2(50)

BEGIN
SELECT NAME INTO name_d FROM ANIMALS A WHERE A.ID = :NEW.ID; 
SELECT BREED INTO breed_d FROM ANIMALS A  WHERE A.ID = :NEW.ID; 
SELECT AREA INTO area_d FROM STORE S WHERE S.STORE_ID = :NEW.STORE_ID;

IF (:NEW.DONE ='T') 
THEN 
:NEW.MSG = 'Hi , your animal ' || name_d || ' breed: ' || breed_d  || 'is 
at ' || area_d || '.';
ELSE 
UPDATE :NEW.MSG = 'Not finished';
END IF;
END;
/

1 个答案:

答案 0 :(得分:0)

您的Trigger代码语法几乎没有问题。

  • 我们在declare not comma中的每个表达式后使用分号';' ','
  • 检查IF条件的正确语法。

  • THEN UPDATE :NEW.MSG =

不是有效的陈述。它只是:NEW.MSG :=

CREATE OR replace TRIGGER tr_history
  BEFORE INSERT ON history
  FOR EACH ROW
DECLARE
    name_d  VARCHAR2(50);
    breed_d VARCHAR2(50);
    area_d  VARCHAR2(50);
BEGIN
    SELECT name
    INTO   name_d
    FROM   animals A
    WHERE  A.id = :NEW.id;

    SELECT breed
    INTO   breed_d
    FROM   animals A
    WHERE  A.id = :NEW.id;

    SELECT area
    INTO   area_d
    FROM   store S
    WHERE  S.store_id = :NEW.store_id;

    IF :NEW.done = 'T' THEN
      :NEW.msg := 'Hi , your animal '
                  || name_d
                  || ' breed: '
                  || breed_d
                  || 'is      at '
                  || area_d
                  || '.';
    ELSE
      :NEW.msg := 'Not finished';
    END IF;
END;

/