【问题标题】:"Trigger --- is invalid and failed re-validation" [closed]“触发器 --- 无效且重新验证失败” [关闭]
【发布时间】:2015-12-08 17:56:28
【问题描述】:
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;

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

【问题讨论】:

  • 我对“新”的来源感到困惑,我认为这可能会导致您的错误。它应该是别名吗?
  • 如果你标记Oracle,也许你会得到更多帮助。
  • :new替换new;在查询之前检查:new.SpervisiorID IS NULL;如有必要,处理“未找到行”异常。如果您从 sqlplus 运行,请键入 show errors 以在编译触发器后获取有关错误的更多详细信息。
  • 您是否有一个名为 oldnew 的字段? show errors trigger Supervisors 的结果是什么?

标签: sql oracle plsql database-trigger


【解决方案1】:

当给定的触发器被创建时,它会报告一个编译错误:

Trigger SUPERVISORS compiled
Errors: check compiler log

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

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

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

此外,尽早失败并使用更细粒度的异常以便于调试总是一个好主意,因此我添加了另外 2 个异常来处理 NULL 场景和 NO_DATA_FOUND 场景,与“坏排名”异常分开处理。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    相关资源
    最近更新 更多