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;
以上是每次运行查询时的触发器,导致我收到错误,指出触发器无效且重新验证失败
答案 0 :(得分:1)
创建给定触发器时,它会报告编译错误:
Trigger SUPERVISORS compiled
Errors: check compiler log
编译错误来自一些缺少的冒号(":"),它应该位于" new"的引用之前。和#34;老" 在触发器体内。
注意:" WHEN"中不需要冒号前缀。触发器的子句在这里:
WHEN(new.EmpRank = 0 OR new.EmpRank = 1)
此外,最好通过更细粒度的异常提前失败以便于调试,因此我已经添加了2个异常来处理NULL场景和NO_DATA_FOUND场景与"排名不良&# 34;异常。
以下是更新的代码,包括测试表定义和示例插入语句:
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