无法弄清楚触发器无效的原因?

时间:2013-03-18 20:13:20

标签: oracle plsql triggers

以下触发器已创建,但在插入时,获取错误“触发器无效且重新验证失败”

 create or replace trigger check_dept
BEFORE INSERT ON employees
FOR EACH ROW
DECLARE
c number(1);
BEGIN
 select count(*) INTO c from departments where departments.department_id=:new.department_id;

IF c = 0 THEN
insert into departments(department_id,department_name) values (:new.department_id,'others');
END IF;

END;

我缺少什么?

3 个答案:

答案 0 :(得分:2)

假设您使用默认的HR架构,触发器编译就好了。

C:\Users\jcave>sqlplus hr/hr

SQL*Plus: Release 11.2.0.1.0 Production on Mon Mar 18 16:55:42 2013

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


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

SQL> create or replace trigger check_dept
  2  BEFORE INSERT ON employees
  3  FOR EACH ROW
  4  DECLARE
  5  c number(1);
  6  BEGIN
  7   select count(*) INTO c from departments where departments.department_id=:new.department_id;
  8
  9  IF c = 0 THEN
 10  insert into departments(department_id,department_name) values (:new.department_id,'others');
 11  END IF;
 12
 13  END;
 14  /

Trigger created.


SQL> ed
Wrote file afiedt.buf

  1  insert into employees( employee_id, first_name, last_name, email, hire_date, job_id, department_id)
  2*    values( -1, 'Justin', 'Cave', 'foo@bar.com', sysdate, 'AC_ACCOUNT', 10 )
SQL> /

1 row created.

如果您的触发器无效,则您发布的触发器与您实际编译的触发器有所不同,或者您没有使用默认的HR架构,在这种情况下您需要告诉它我们你的桌子是什么样的。

答案 1 :(得分:0)

也许您需要在代码末尾添加“COMMIT”。

答案 2 :(得分:0)

也许是一个愚蠢的想法,但你有权访问你的触发器访问的视图或表吗?如果它是一个视图,则可能必须添加一个授权。