如何在一定条件下创建更新触发器?

时间:2018-10-23 19:08:19

标签: sql oracle oracle11g database-trigger

我想创建一个触发条件的触发器,该条件要求声明 详细信息只能在放置后三天内更改

CREATE TRIGGER BEFORE UPDATE

CREATE TRIGGER INSTEAD OF UPDATE

我有OrderIdDetailsOrderDate

我该怎么做?你能帮忙吗

2 个答案:

答案 0 :(得分:4)

这是一个例子。

首先,测试用例:

SQL> alter session set nls_date_format = 'dd.mm.yyyy';

Session altered.

SQL> create table test
  2    (orderid   number primary key,
  3     details   varchar2(20),
  4     orderdate date
  5    );

Table created.

SQL> insert into test (orderid, details, orderdate)
  2    select 1, 'test 1', date '2018-10-15' from dual union all
  3    select 2, 'test 2', date '2018-10-22' from dual;

2 rows created.

SQL> select sysdate from dual;

SYSDATE
----------
23.10.2018

SQL> select * from test;

   ORDERID DETAILS              ORDERDATE
---------- -------------------- ----------
         1 test 1               15.10.2018
         2 test 2               22.10.2018

SQL>

触发:

SQL> create or replace trigger trg_bu_test
  2    before update of details on test
  3    for each row
  4  begin
  5    if trunc(sysdate) - :new.orderdate > 3 then
  6       raise_application_error(-20001, 'More than 3 days have passed; update is not allowed');
  7    end if;
  8  end;
  9  /

Trigger created.

测试:

SQL> -- ID = 1 - 8 days have passed, no update is allowed
SQL> update test set details = 'xxx' where orderid = 1;
update test set details = 'xxx' where orderid = 1
       *
ERROR at line 1:
ORA-20001: More than 3 days have passed; update is not allowed
ORA-06512: at "SCOTT.TRG_BU_TEST", line 3
ORA-04088: error during execution of trigger 'SCOTT.TRG_BU_TEST'


SQL> -- ID = 2 - 1 day passed - update is allowed
SQL> update test set details = 'yyy' where orderid = 2;

1 row updated.

SQL>

P.S。 INSTEAD OF触发器(您在问题中提到的)与视图一起使用;忘掉它们(在这种情况下)。

答案 1 :(得分:0)

尝试使用AFTER触发器

create or replace TRIGGER TR_details_update
AFTER UPDATE ON order_det
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW

BEGIN
    IF UPDATING THEN 
        IF  NVL(:OLD.Details, 'XXX')     <>   NVL(:NEW.Details, 'XXX') AND :OLD.OrderDate <= TRUNC(SYSDATE) - 3  THEN            
               update order_det
               set Details = :new.Details
               where Details = :old.Details 
               and OrderId = :old.OrderId;
        END IF;
    END IF;
        EXCEPTION
        WHEN OTHERS THEN
    RAISE_APPLICATION_ERROR (-20000,'Details must be changed within 3 days of order date');
END TR_details_update;