Get current time, on commit, with oracle database

oracleoracle-12cplsql

When inserting a row I want to make a column, equal to the current time on commit.

If I use default value sysdatetime on the column, it uses the current time at insert (which could be significantly different to commit time). If I use an on insert trigger, it again uses the time at the insert – not the actual commit.

Are there any possible solutions to get current time on commit with oracle ?

Best Answer

To get an approximate value (+/- 3sec or so) you may use ORA_ROWSCN

You must define the table with ROWDEPENDENCIES to have the SCN stored for each row.

Here an simple example:

create table t1 
(c1 number,
c2 date)
ROWDEPENDENCIES;

insert into t1 values(1,sysdate);
-- wait 5 sec
commit;

insert into t1 values(2,sysdate);
-- wait 10 sec
commit;

SELECT ORA_ROWSCN, SCN_TO_TIMESTAMP(ORA_ROWSCN), c1,c2 FROM t1;


ORA_ROWSCN SCN_TO_TIMESTAMP(ORA_ROWSCN)         C1 C2                
---------- ---------------------------- ---------- -------------------
   1.4E+13 19.05.16 19:14:15,000000000           1 19.05.2016 19:14:09 
   1.4E+13 19.05.16 19:14:30,000000000           2 19.05.2016 19:14:21