2

正在创建以下触发器,但在插入时,出现错误“触发器无效并且重新验证失败”

 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;

我错过了什么?

4

3 回答 3

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模式,在这种情况下,您需要告诉我们您的表是什么样的。

于 2013-03-18T21:01:23.520 回答
0

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

于 2013-03-18T21:48:32.353 回答
0

也许是一个愚蠢的想法,但您是否有权访问触发器正在访问的视图或表?如果它是一个视图,那么也许必须添加一个授权。

于 2013-03-18T22:10:46.383 回答