As part of audit/history functionality I want to handle following scenario using AFTER UPDATE trigger or any other trigger, please let me know. Scenario --
- There will 2 tables -- Base table and history table
- On update of any record in Base table 1st insert same record (to be updated) in History table with old values.
- Update record with new values in Base table.
I am using following trigger, giving deadlock scenario. Please advice to resolve this issue.
create table Base_table(
SYMBOL_ID NUMBER(9) primary key,
SYMBOL_NAME VARCHAR2(20) ,
PRICE NUMBER(9) ,
VERSION NUMBER(1)
)
organization index;
create table base_table_hist(
ID NUMBER(9) primary key,
SYMBOL_ID NUMBER(9) ,
SYMBOL_NAME VARCHAR2(20) ,
PRICE NUMBER(9),
VERSION NUMBER(1) ,
constraint other_symbolid foreign key(symbol_id) references test_symbol(symbol_id)
)
organization index;
************************************************************
create or replace Trigger Symbol_Ver
AFTER UPDATE ON Base_table
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
DECLARE
new_version number(5);
--Pragma AUTONOMOUS_TRANSACTION;
Sid number(9);
begin
if (:New.symbol_id <> :Old.symbol_id) OR (:New.price <> :Old.price) then
new_version:= :Old.version+1;
--insert into history table
insert into base_table_hist (id, symbol_id, symbol_name,price,version)
values (symbol_seq.nextval, :OLD.symbol_id, :OLD.symbol_name, :OLD.price, :OLD.version);
commit;
DBMS_OUTPUT.put_line('new_version..'||new_version);
end if;
if (:New.symbol_id <> :Old.symbol_id) OR (:New.price <> :Old.price) then
update base_table set version=new_version where symbol_id=:Old.symbol_id;
end if;
end;
:new.version
), and creating the history record, in abefore
trigger rather than doing extra DML? – Alex Poole