更新触发器(如何)

时间:2017-12-04 18:52:21

标签: plsql sql-update alter eventtrigger

有人可以帮助更新触发器吗?我知道我应该使用alter子句,但之后我被卡住了。我想改变这个触发器:

CREATE OR REPLACE TRIGGER  "CR_SURGERY_AUDIT"  
before insert or delete or update on CR_SURGERY
for each row
declare 
V_user varchar2 (30);
V_date varchar2 (30); 
begin 
select user, to_char(sysdate, 'dd/mon/yyyy hh24:mi:SS') INTO v_user, v_date
from dual;
IF inserting THEN
insert into CR_SURGERY_AUDIT (new_name, old_name, user_name, entry_date, operation)
VALUES (:NEW.CR_SURGERY, NULL, v_user, v_date, 'insert');
ELSIF DELETING THEN
insert into CR_SURGERY_AUDIT (new_name, old_name, user_name, entry_date, operation)
VALUES (null, :OLD.CR_SURGERY, v_user, v_date, 'delete');
ELSIF UPDATING THEN
insert into CR_SURGERY_AUDIT (new_name, old_name, user_name, entry_date, operation)
VALUES (:NEW.CR_SURGERY, :OLD.CR_SURGERY, v_user, v_date, 'Update');
END IF;
END;

我知道这很简单,但我找不到合成方法。我想添加FK_SOCRD_ID,以便更新为:

select user, to_char(sysdate, 'dd/mon/yyyy') INTO v_user, v_date from dual;
IF inserting THEN
insert into CR_SURGERY_AUDIT (new_name, old_name, user_name, entry_date,
operation, FK_SOCRD_ID)  
VALUES (:NEW.CR_SURGERY, NULL, v_user, v_date, 'insert');

1 个答案:

答案 0 :(得分:0)

没有修改触发器的语法,只需修改create or replace trigger代码并重新运行即可。

根据您提议的更改,我会这样做:

create or replace trigger cr_surgery_audit
before insert or delete or update on cr_surgery
for each row
declare
    k_user constant cr_surgery_audit.user_name%type := sys_context('userenv','os_user');
    k_date constant cr_surgery_audit.entry_date%type := sysdate;
begin
    if inserting then
        insert into cr_surgery_audit (new_name, old_name, user_name, entry_date, operation, fk_socrd_id)  
        values (:new.cr_surgery, null, k_user, k_date, 'insert', 12345);
    elsif deleting then
        insert into cr_surgery_audit (new_name, old_name, user_name, entry_date, operation)
        values (null, :old.cr_surgery, k_user, k_date, 'delete');
    elsif updating then
        insert into cr_surgery_audit (new_name, old_name, user_name, entry_date, operation)
        values (:new.cr_surgery, :old.cr_surgery, k_user, k_date, 'update');
    end if;
end;

我认为cr_surgery_audit.entry_date实际上是date而不是varchar2

您需要一个值来填充fk_socrd_id,因此我将123456作为虚拟值。