database - Oracle 触发器无效

标签 database oracle triggers plsql

我是 SQL 的新手,我正在尝试创建一个将插入到审计表中的触发器。

create or replace trigger late_ship_insert
  after insert on suborder
  for each row
declare
  employee int;
begin
  select emp_id 
    into employee
    from handles
   where order_no = :new.order_no;
  if :new.actual_ship_date > :new.req_ship_date then
    insert into ship_audit
      values (employee, :new.order_no, :new.suborder_no, :new.req_ship_date, :new.actual_ship_date);
end;

错误:

Warning: execution completed with warning
trigger late_ship_insert Compiled.

但是一旦我尝试插入语句,它就会告诉我触发器无法正常工作以删除它。

Error starting at line 1 in command:
insert into suborder 
    values  ( 8, 3, '10-jun-2012', '12-jul-2012', 'CVS', 3) 
Error at Command Line:1 Column:12
Error report:
SQL Error: ORA-04098: trigger 'COMPANY.LATE_SHIP_INSERT' is invalid and failed re-validation
04098. 00000 -  "trigger '%s.%s' is invalid and failed re-validation"
*Cause:    A trigger was attempted to be retrieved for execution and was
           found to be invalid.  This also means that compilation/authorization
           failed for the trigger.
*Action:   Options are to resolve the compilation/authorization errors,
           disable the trigger, or drop the trigger.

知道是什么原因造成的,如有任何帮助,我们将不胜感激。谢谢!

最佳答案

格式化代码时出现的明显错误是您的 IF 语句缺少 END IF

create or replace trigger late_ship_insert
  after insert on suborder
  for each row
declare
  employee int;
begin
  select emp_id 
    into employee
    from handles
   where order_no = :new.order_no;
  if :new.actual_ship_date > :new.req_ship_date then
    insert into ship_audit
      values (employee, :new.order_no, :new.suborder_no, :new.req_ship_date, :new.actual_ship_date);
  end if;
end;

一般来说,您应该始终在您的 INSERT 语句中列出目标表的列,而不是依赖于您的 INSERT 语句为每一列并以正确的顺序指定它们。这将使您的代码更加健壮,因为当有人向表中添加其他列时它不会变得无效。这看起来像这样(我在猜测 ship_audit 表中列的名称)

create or replace trigger late_ship_insert
  after insert on suborder
  for each row
declare
  employee int;
begin
  select emp_id 
    into employee
    from handles
   where order_no = :new.order_no;
  if :new.actual_ship_date > :new.req_ship_date then
    insert into ship_audit( emp_id, order_no, suborder_no, req_ship_date, actual_ship_date )
      values (employee, :new.order_no, :new.suborder_no, :new.req_ship_date, :new.actual_ship_date);
  end if;
end;

关于database - Oracle 触发器无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11569081/

相关文章:

php - 为什么我不能在 Wordpress 中创建新帖子?第 716 行的警告 : Creating default object from empty value in/public_html/wp-admin/includes/post. php

oracle - PL/SQL异常处理-函数返回无值

oracle - ORA-02069 global_names 参数必须为此操作设置为 TRUE

java - 在mysql 5.5中通过JDBC创建触发器时出错

azure - 计时器触发的Azure函数不规则执行

php - uniqid(”, true) 的主要功能是什么?

python - python中的数据可视化——连接到数据库后

database - 如何更新多对多链接表中的数据?

sql - 如何使用 Form Builder 和 PL/SQL 插入记录?

MySQL 更新触发器 - 查找更改的列?