如何在触发器内调用/调用序列

时间:2018-11-28 13:39:06

标签: sql oracle database-trigger

我想自动增加我创建的表ID。该表将在我的数据库中保存有关从某个表进行的更改的日志。以下是我的代码和出现的错误。请帮助我,谢谢你。

$s("P165_GET_PK",this.triggeringElement.value)

-----下面是我创建的序列-----

create or replace Trigger PVFC_audit
after insert or delete or update on customer
for each row 
declare
v_user varchar2(20);
eventlog_id_go number;
v_date date;
v_time timestamp;
act_done varchar2(20) := case when updating then 'Update' when deleting then 'Delete' else 'Insert' end;
begin
IF eventlog_id IS NULL THEN  <--- eventlog_id is the actual column in my eventlogs table
SELECT eventlog_id_seq.NEXTVAL , user, To_Char(sysdate,'DD/MON/YYY'), To_char(CURRENT_TIMESTAMP,'HH12:MI:SS')
INTO eventlog_id_go, v_user, v_date, v_time from dual;
END IF;
if inserting then
    insert into eventlogs(eventlog_id, user_name, date_done, time_done, action_done, object_name)
    values (eventlog_id_go, v_user, v_date, v_time, act_done , 'customer');
elsif deleting then
    insert into eventlogs(eventlog_id, user_name, date_done, time_done, action_done, object_name)
    values (eventlog_id_go, v_user, v_date, v_time, act_done,'customer');
elsif updating then
    insert into eventlogs(eventlog_id, user_name, date_done, time_done, action_done, object_name)
    values (eventlog_id_go, v_user, v_date, v_time, act_done ,'customer');
end if;
end;

错误(13,4):PLS-00201:必须声明标识符“ EVENTLOG_ID”

2 个答案:

答案 0 :(得分:3)

  

eventlog_id是另一个名为eventlogs的表中的列

因此您不能在触发器中引用它:触发器只看到拥有它的表的列。该解决方案仅填充INSERT语句中的eventlog_id列。

偶然地,您的触发器有很多重复。您可以极大地简化:

create or replace Trigger PVFC_audit
after insert or delete or update on customer
for each row 
declare
    act_done varchar2(20) := case when updating then 'Update' when deleting then 'Delete' else 'Insert' end;
begin

    if inserting then
        act_done := 'INSERT';
    elsif deleting then
        act_done := 'DELETE';
    elsif updating then
        act_done := 'UPDATE';
    end if;
    insert into eventlogs(eventlog_id, user_name, date_done, time_done, action_done, object_name)
    values (eventlog_id_seq.NEXTVAL , user, To_Char(sysdate,'DD/MON/YYY'), To_char(CURRENT_TIMESTAMP,'HH12:MI:SS')
    , act_done ,'customer');

end;

不确定为什么将date_donetime_done填充为字符串。使用正确的数据类型会更有意义。

    insert into eventlogs(eventlog_id, user_name, date_done, time_done, action_done, object_name)
    values (eventlog_id_seq.NEXTVAL , user, trunc(sysdate), CURRENT_TIMESTAMP
    , act_done ,'customer');

如果EVENTLOGS将那些列定义为VARCHAR2,那么几乎可以肯定这是一个数据模型错误,在某些时候会让您感到悲伤。

答案 1 :(得分:0)

您需要引用pseudorecords旧值/新值

IF :OLD.eventlog_id IS NULL THEN 

IF :NEW.eventlog_id IS NULL THEN