2
CREATE TRIGGER Supervisors
BEFORE INSERT OR UPDATE ON Employee
FOR EACH ROW
WHEN(new.EmpRank = 0 OR new.EmpRank = 1)
DECLARE
    supervisorRank INT;
BEGIN
    SELECT EmpRank INTO supervisorRank
    FROM Employee
    WHERE new.SupervisorID = Employee.Id;

    IF((new.SupervisorID IS NULL) OR (supervisorRank - new.EmpRank != 1)) THEN
        RAISE_APPLICATION_ERROR(-20004, 'Cannot insert/update record into table Employee. Invalid supervisor.');
    END IF;
END;

以上是我每次运行查询时的触发器,它导致我收到一个错误,提示触发器无效并且重新验证失败

4

1 回答 1

1

创建给定触发器时,它会报告编译错误:

Trigger SUPERVISORS compiled
Errors: check compiler log

编译错误来自一些丢失的冒号(“:”),这些冒号应该位于触发器主体中对“新”和“旧”的引用之前。

注意:此处触发器的“WHEN”子句中不需要冒号前缀:

WHEN(new.EmpRank = 0 OR new.EmpRank = 1)

此外,尽早使用更精细的异常以更容易调试总是一个好主意,因此我添加了另外 2 个异常来处理 NULL 场景和 NO_DATA_FOUND 场景,与“bad rank”异常分开处理。

这是更新的代码,包括测试表定义和示例插入语句:

drop table employee
/

create table employee ( 
  id number,
  EmpRank number,
  SupervisorID number,
  primary key(id))
/

CREATE or replace TRIGGER Supervisors
BEFORE INSERT OR UPDATE ON Employee
FOR EACH ROW
WHEN(new.EmpRank = 0 OR new.EmpRank = 1)
DECLARE
    supervisorRank INT;
BEGIN
  if (:new.SupervisorID is null) then
    RAISE_APPLICATION_ERROR(-20006, 'Cannot insert/update record into table Employee. Required supervisor ID is missing.');
  end if;

  begin
    SELECT EmpRank INTO supervisorRank
    FROM Employee
    WHERE :new.SupervisorID = Employee.Id;
  exception when no_data_found then
    RAISE_APPLICATION_ERROR(-20005, 'Cannot insert/update record into table Employee. Supervisor ID not found.');
  end;

  IF (supervisorRank - :new.EmpRank != 1) THEN
    RAISE_APPLICATION_ERROR(-20004, 'Cannot insert/update record into table Employee. Employee rank is not valid for given supervisor.');
  END IF;
END;
/

insert into employee values ( 6, 1, null );
insert into employee values ( 5, 1, 6 );
insert into employee values ( 4, 2, null );
insert into employee values ( 3, 1, 4 );
insert into employee values ( 2, 0, 3 );
insert into employee values ( 1, 0, 3 );
insert into employee values ( 0, 0, 4 );

exit
/

下面是上述脚本的输出,显示了各种异常消息。请注意,我使用的是新的 SQLcl (sql.exe),而不是 SQL*Plus,但结果应该相同。

SQLcl: Release 4.2.0.15.295.1605 RC on Tue Dec 08 17:07:23 2015

Copyright (c) 1982, 2015, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options


Table EMPLOYEE dropped.


Table EMPLOYEE created.


Trigger SUPERVISORS compiled


Error starting at line : 36 in command -
insert into employee values ( 6, 1, null )
Error report -
SQL Error: ORA-20006: Cannot insert/update record into table Employee. Required supervisor ID is missing.
ORA-06512: at "APPS.SUPERVISORS", line 5
ORA-04088: error during execution of trigger 'APPS.SUPERVISORS'


Error starting at line : 37 in command -
insert into employee values ( 5, 1, 6 )
Error report -
SQL Error: ORA-20005: Cannot insert/update record into table Employee. Supervisor ID not found.
ORA-06512: at "APPS.SUPERVISORS", line 13
ORA-04088: error during execution of trigger 'APPS.SUPERVISORS'


1 row inserted.


1 row inserted.


1 row inserted.


1 row inserted.


Error starting at line : 42 in command -
insert into employee values ( 0, 0, 4 )
Error report -
SQL Error: ORA-20004: Cannot insert/update record into table Employee. Employee rank is not valid for given supervisor.
ORA-06512: at "APPS.SUPERVISORS", line 17
ORA-04088: error during execution of trigger 'APPS.SUPERVISORS'

Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
于 2015-12-08T23:16:02.657 回答