Skip to content

Instantly share code, notes, and snippets.

@sakshatshinde
Last active January 9, 2023 15:11
Show Gist options
  • Save sakshatshinde/18572a3e73af0519e66fb6499799aba6 to your computer and use it in GitHub Desktop.
Save sakshatshinde/18572a3e73af0519e66fb6499799aba6 to your computer and use it in GitHub Desktop.
How to check historic values of a cell in Oracle SQL DB

Few ways to peek into your DB's history

SELECT salary FROM emp AS OF TIMESTAMP TIMESTAMP '2021-12-31 23:59:59.999999999' WHERE  id = 1111

SELECT deptno, dname, VERSIONS_STARTTIME, VERSIONS_XID, VERSIONS_OPERATION 
FROM dept VERSIONS BETWEEN TIMESTAMP SYSTIMESTAMP - INTERVAL '20:00' MINUTE TO SECOND AND SYSTIMESTAMP 
WHERE deptno = 10;

SELECT * FROM table_a WHERE ora_rowscn >= timestamp_to_scn(trunc(sysdate));

It depends how the DBA has configured the database. History is tracked by a number - SCN which is basically a rollback entry automatically created whenever you do any DML.

Detailed explanation on how this all works: Time travel with SQL!

What is SCN?


Misc info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment