更新触发器(如何)

Updating trigger (how)

有人可以帮助更新触发器吗?我知道我应该使用 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');

没有修改触发器的语法,您只需修改 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 作为虚拟值。