0

I've written a CREATE TRIGGER but for some reason, I can't get it to not have a complier error. Any ideas? Here's the code and the error message:

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;

The error message is:

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. 

EDIT: Here's a clarification of my problem. I'm creating a table with the following statements. Each employee has a manager (which is represented by the 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)
);

I then want to create a trigger that whenever an employee A changes his jobTitle, it finds all employees that had A as their manager and sets the manager field to null. Does this make any sense?

4

2 回答 2

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;
/
于 2013-10-10T18:06:54.243 回答
0

尝试将其包装在开始和结束子句中。

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;
于 2013-10-10T18:15:01.437 回答