SELECT …
FROM [schema.]table [alias]
[
AS OF { SCN | TIMESTAMP } expr
| VERSIONS BETWEEN { SCN | TIMESTAMP}
{ expr | MINVALUE } AND { expr | MAXVALUE }
]
[ WHERE … ]
[ GROUP BY … ]
[ ORDER BY … ]
“AS OF” will take you to back in time to a previous state of a table against a time or system change number. For example we want to see the records in a table 10 minutes ago then we can use the “AS OF" condition with the SCN or timestamp with additional conditions, if required, like “where” / ”group by” / “order by” . The “VERSIONS BETWEEN” will take you between ranges of time where a table was earlier. In this we can get multiple sets of versions or records of the table with expressions like MAX or MIN values of any columns.
Connected to Oracle Database 11g Express Edition Release 11.2.0.2.0
Connected as sloba
SQL>
SQL> create table link_details
2 (
3 id number ,
4 name varchar2(500) ,
5 site_url varchar2(4000)
6 );
Table created
SQL> insert into link_details values(1, 'Oracle', 'https://www.microsoft.com') ;
1 row inserted
SQL> insert into link_details values(2, 'Microsoft','http://www.oracle.com/index.html');
1 row inserted
SQL> commit;
Commit complete
SQL>
SQL> select * from link_details;
ID NAME SITE_URL
---------- ------------------------ --------------------------------------
1 Oracle https://www.microsoft.com
2 Microsoft http://www.oracle.com/index.html
SQL>
SQL>
SQL> SELECT id
,NAME
,to_char(versions_starttime
,'DD-MON-YYYY HH24:MI:SS') AS stime
,to_char(versions_endtime
,'DD-MON-YYYY HH24:MI:SS') "ETime"
,versions_startscn "SSCN"
,versions_endscn "ESCN"
,versions_operation
,decode(versions_operation
,'I'
,'Insert'
,'U'
,'Update'
,'D'
,'Delete'
,'Error') "DML"
FROM link_details versions BETWEEN TIMESTAMP minvalue AND maxvalue
ORDER BY stime NULLS FIRST;
ID NAME STIME ETime SSCN ESCN VERSIONS_OPERATION DML
---------- ----------------------------------------------- --------------------- ---------- ------------------ ------
1 Oracle 30-JUN-2015 19:18:58 767945 I Insert
2 Microsoft 30-JUN-2015 19:18:58 767945 I Insert
SQL>
SQL> insert into link_details values(3, 'Experts-Exchange','http://www.experts-exchange.com');
1 row inserted
SQL> commit;
Commit complete
SQL> select * from link_details;
ID NAME SITE_URL
---------- --------------------- --------------------------------------
1 Oracle https://www.microsoft.com
2 Microsoft http://www.oracle.com/index.html
3 Experts-Exchange http://www.experts-exchange.com
SQL>
SQL> SELECT * FROM link_details AS OF TIMESTAMP(SYSTIMESTAMP - INTERVAL '1' MINUTE);
ID NAME SITE_URL
---------- -----------------------------------------------------------
1 Oracle https://www.microsoft.com
2 Microsoft http://www.oracle.com/index.html
3 Experts-Exchange http://www.experts-exchange.com
SQL>
SQL> SELECT * FROM link_details
2 AS OF TIMESTAMP(SYSTIMESTAMP - INTERVAL '3' MINUTE);
ID NAME SITE_URL
---------- ------------------------------------------------------
1 Oracle https://www.microsoft.com
2 Microsoft http://www.oracle.com/index.html
SQL>
From the above two flashback queries we can see that the time difference provides the details on what records were present before certain period of time.
SQL> update link_details set NAME = 'Oracle Corporation' where ID = 1;
1 row updated
SQL> commit;
Commit complete
SQL> select * from link_details;
ID NAME SITE_URL
---------- --------------------- ------------------------------------
1 Oracle Corporation https://www.microsoft.com
2 Microsoft http://www.oracle.com/index.html
3 Experts-Exchange http://www.experts-exchange.com
SQL>
SQL>
SQL> SELECT id
,NAME
,to_char(versions_starttime
,'DD-MON-YYYY HH24:MI:SS') AS stime
,to_char(versions_endtime
,'DD-MON-YYYY HH24:MI:SS') "ETime"
,versions_startscn "SSCN"
,versions_endscn "ESCN"
,versions_operation
,decode(versions_operation
,'I'
,'Insert'
,'U'
,'Update'
,'D'
,'Delete'
,'Error') "DML"
FROM link_details versions BETWEEN TIMESTAMP minvalue AND maxvalue
ORDER BY stime NULLS FIRST;
ID NAME STIME ETime SSCN ESCN VERSIONS_OPERATION DML
---------- ----------------------- ----------------------------- ----------------------------- ---------- ---------- ------------------ ------
1 Oracle 30-JUN-2015 19:57:50 768889 Error
2 Microsoft Error
3 Experts-Exchange 30-JUN-2015 19:51:20 768748 I Insert
1 Oracle Corporation 30-JUN-2015 19:57:50 768889 U Update
SQL>
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)