Create History Table For Existing Table
Create History Table For Existing Table
Create History Table For Existing Table
-- triggers
create or replace trigger blue_biu
before insert or update
on blue
for each row
begin
if :new.id is null then
:new.id := to_number(sys_guid(), 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
end if;
if inserting then
:new.created := sysdate;
:new.created_by := nvl(sys_context('APEX$SESSION','APP_USER'),user);
end if;
:new.updated := sysdate;
:new.updated_by := nvl(sys_context('APEX$SESSION','APP_USER'),user);
end blue_biu;
/
-- history tracking
create sequence history_seq_blue;
create table history_blue (
id number primary key,
table_name varchar2(128),
column_name varchar2(128),
action varchar2(1) check (action in ('I','U','D')),
action_date date,
action_by varchar2(255),
data_type varchar2(255),
pk1 number,
tab_row_version integer,
old_vc varchar2(4000),
new_vc varchar2(4000),
old_number number,
new_number number,
old_date date,
new_date date,
old_ts timestamp,
new_ts timestamp,
old_tswtz timestamp with time zone,
new_tswtz timestamp with time zone,
old_tswltz timestamp with local time zone,
new_tswltz timestamp with local time zone,
old_clob clob,
new_clob clob,
old_blob blob,
new_blob blob
)
/
end if;
if (:old.bname is null and :new.bname is not null) or
(:old.bname is not null and :new.bname is null) or
:old.bname != :new.bname then
insert into history (
id, table_name, column_name, pk1, tab_row_version, action, action_date,
action_by, data_type, old_vc, new_vc
) values (
history_seq.nextval, t, 'BNAME', :old.id, null, 'U', sysdate, u,
'VARCHAR2', :old.bname, :new.bname);
end if;
if (:old.btag is null and :new.btag is not null) or
(:old.btag is not null and :new.btag is null) or
:old.btag != :new.btag then
insert into history (
id, table_name, column_name, pk1, tab_row_version, action, action_date,
action_by, data_type, old_vc, new_vc
) values (
history_seq.nextval, t, 'BTAG', :old.id, null, 'U', sysdate, u,
'VARCHAR2', :old.btag, :new.btag);
end if;
elsif deleting then
insert into history (
id, table_name, column_name, pk1, tab_row_version, action, action_date,
action_by, data_type, old_number, new_number
) values (
history_seq.nextval, t, 'ID', :old.id, null, 'D', sysdate, u, 'NUMBER',
:old.id, :new.id);
insert into history (
id, table_name, column_name, pk1, tab_row_version, action, action_date,
action_by, data_type, old_vc, new_vc
) values (
history_seq.nextval, t, 'BNAME', :old.id, null, 'D', sysdate, u,
'VARCHAR2', :old.bname, :new.bname);
insert into history (
id, table_name, column_name, pk1, tab_row_version, action, action_date,
action_by, data_type, old_vc, new_vc
) values (
history_seq.nextval, t, 'BTAG', :old.id, null, 'D', sysdate, u, 'VARCHAR2',
:old.btag, :new.btag);
end if;
end blue_aud;
/
-- load data