我写了一个CREATE TRIGGER,但由于某种原因,我无法让它没有编译错误。有任何想法吗?这是代码和错误消息:
CREATE OR REPLACE TRIGGER ManagerDeleteTrigger
AFTER DELETE ON employee1
REFERENCING
OLD AS OldRow
NEW AS NewRow
FOR EACH ROW
WHEN(OldRow.FK_EMPLOYEEEMPLOYEEID != NewRow.FK_EMPLOYEEEMPLOYEEID)
UPDATE employee1 SET FK_EMPLOYEEEMPLOYEEID = null WHERE FK_EMPLOYEEEMPLOYEEID = OldRow.employeeID;
错误消息是:
Error(1,105): PLS-00103: Encountered the symbol ";" when expecting one of the following:
( begin case declare end exception exit for goto if loop mod null pragma raise return select update while with <an identifier> <a double-quoted delimited-identifier> <a bind variable>
<< continue close current delete fetch lock insert open rollback savepoint set sql execute
commit forall merge pipe purge The symbol "exit" was substituted for ";" to continue.
编辑:这是对我的问题的澄清。我正在创建一个包含以下语句的表。每位员工都有一位经理(由FK代表)。
CREATE TABLE Employee1
(
employeeID integer,
firstName varchar (255),
lastName varchar (255),
phone integer,
jobTitle varchar (255),
payGrade integer,
fk_EmployeeemployeeID integer NOT NULL,
PRIMARY KEY(employeeID),
FOREIGN KEY(fk_EmployeeemployeeID) REFERENCES Employee1 (employeeID)
);
然后,我想创建一个触发器,每当员工A更改其jobTitle时,它会找到所有员工将A作为其经理并将manager字段设置为null。这有什么意义吗?
答案 0 :(得分:2)
您错过了实际触发器代码周围的BEGIN ... END
块。请查看手册,其中记录了完整的语法。
下一个错误是您无法UPDATE
正在更新的表。您只需将新值分配给相关列:
另一个问题是AFTER
触发器无法更改任何值,您需要使用BEFORE
触发器。
最后,为什么要更改DELETE
触发器中的值?无论如何,该行将在删除后消失,因此无需更改该值。您可能想要使用UPDATE触发器:
CREATE OR REPLACE TRIGGER ManagerDeleteTrigger
BEFORE UPDATE ON employee1
REFERENCING OLD AS OldRow
NEW AS NewRow
FOR EACH ROW
WHEN (OldRow.FK_EMPLOYEEEMPLOYEEID != NewRow.FK_EMPLOYEEEMPLOYEEID)
BEGIN
:NewRow.FK_EMPLOYEEEMPLOYEEID := null;
END;
/
答案 1 :(得分:0)
尝试将其包装在begin和end子句中。
CREATE OR REPLACE TRIGGER ManagerDeleteTrigger
AFTER DELETE ON employee1
REFERENCING
OLD AS OldRow
NEW AS NewRow
BEGIN
FOR EACH ROW
WHEN(OldRow.FK_EMPLOYEEEMPLOYEEID != NewRow.FK_EMPLOYEEEMPLOYEEID)
UPDATE employee1 SET FK_EMPLOYEEEMPLOYEEID = null WHERE FK_EMPLOYEEEMPLOYEEID = OldRow.employeeID
END ManagerDeleteTrigger;