You are on page 1of 6

Oracle

Oracle's Flashback Query - TIMESTAMP or


SCN?
By 1ames Koopmann
Oracles Flashback Query (SQL-driven) makes use of both TIMESTAMP and SCN--but which
should you use? It fust may be a matter of preference but requires some thoughtful
considerations.
In order to take advantage oI Oracle`s Ilashback query Ieature, Ior SQL-driven Ilashback, the
SELECT statement makes use oI the AS OF or VERSIONS BETWEEN clause to retrieve data
Irom the past Irom tables, views, or materialized views.
SELECT ... FRJM ...
VERSIJNS BETWEEN , SCN | TIMESTAMP , , expr | MINVALUE ,
AND , expr | MAXVALUE , ,
-or-
AS JF , SCN | TIMESTAMP , expr
Both the VERSIONS BETWEEN and the AS OF clause requires the user to code in the SQL an
SCN or TIMESTAMP Ior proper retrieval oI past inIormation. The question oIten becomes
should I use an SCN or should I use a TIMESTAMP? The answer is not always clear but I`ll
investigate both here to help you grapple with your next decision.
First, it makes complete sense to deIine what a TIMESTAMP and an SCN is:
#elated Articles
O Oracle's Flashback Query - Should you unset inmemoryundo?
TIMESTAMP is an extension oI the DATE datatype, which stores all the inIormation that the
DATE datatype stores (month, day, year, hour, minute, second) but also includes Iractional
seconds. Personally, I Iind this a bit conIusing since date, by the strictest deIinition, is JUST a
particular month, day, and year when an event happened. Regardless, since TIMESTAMP is the
datatype required Ior a SQL-driven the Ilashback query, the Iollowing SQL conversions could
come in handy:
Just as there is a call to SYSDATE to get the current system date and time, there is a call to get
the current system TIMESTAMP.
SQL SELECT SYSTIMESTAMP FRJM dual;
SYSTIMESTAMP
-------------------------------------
-JUN-09 0..52.622699 AM -06:00
IF you're interested in the current session TIMSTAMP in relation to the session time zone
(SESSIONTIMEZONE) use the CURRENTTIMESTAMP Iunction call.
SQL SELECT CURRENT_TIMESTAMP FRJM dual;
CURRENT_TIMESTAMP
-------------------------------------------
-JUN-09 0..02.5024 AM -06:00
Moreover, the old standby is to convert character datatypes Ior a known timestamp to a
TIMESTAMP datatype:
SQL SELECT TJ_TIMESTAMP ('200906 :05:00', 'YYYYMMDD HH24:MI:SS') FRJM
dual;
TJ_TIMESTAMP('200906:05:00','YYYYMMDDHH24:MI:SS')
-----------------------------------------------------
-JUN-09 .05.00.000000000 AM
II you must use sysdate, which isn't the best option, you would have to do:
SQL SELECT TJ_TIMESTAMP(TJ_CHAR(sysdate,'YYYYMMDD HH24:MI:SS'),'YYYYMMDD
HH24:MI:SS') FRJM dual;
TJ_TIMESTAMP(TJ_CHAR(SYSDATE,'YYYYMMDDHH24:MI:SS'),'YYYYMMDDHH24:MI:SS')
---------------------------------------------------------------------------
-JUN-09 0.0..000000000 AM
The use oI TIMESTAMPs become very important iI you know a particular point in time or want
to look Ior something within a time period as shown in the Iollowing two SQL statements.
To look at what a speciIic value was at a point in time; say 10 minutes ago:
SELECT
FRJM flash_test
AS JF TIMESTAMP (SYSTIMESTAMP - INTERVAL '0' MINUTE);
To look at what values were during a particular time period; say between 10 minutes ago and
now:
SELECT
FRJM flash_test
VERSIJNS BETWEEN TIMESTAMP
SYSTIMESTAMP - INTERVAL '0' MINUTE AND SYSTIMESTAMP;
SCN (System Change Number) is nothing more than a stamp (number) that deIines a committed
version oI a database at a point in time. This means that every committed transaction is assigned
a unique SCN. Worthy to note, Oracle will also perIorm internal work that generates SCNs. I
think TIMESTAMP has probably overtaken the use oI SCN mostly because many just don't
know where to extract a valid SCN Irom. Here are a Iew areas that could help you in this
endeavor.
Current SCN oI the database.

SQL SELECT DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER() FRJM DUAL;
DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER()
-----------------------------------------
2249659

SQL SELECT CURRENT_SCN FRJM V$DATABASE;
CURRENT_SCN
-----------
2249662
Just remember that there are literally 100's oI tables and views within Oracle that maintain some
Iorm oI an SCN. Use the Iollowing SQL statement to Iind which ones you might want to use Ior
your next Ilashback query.
SELECT owner, table_name, column_name
FRJM dba_tab_columns
WHERE column_name LIKE '%SCN%'

JWNER TABLE_NAME CJLUMN_NAME
----- ------------------------------ ------------------------------
SYS V_$DATABASE CURRENT_SCN
SYS V_$DATABASE STANDBY_BECAME_PRIMARY_SCN
SYS V_$DATAFILE FIRST_NJNLJGGED_SCN
SYS V_$FILESPACE_USAGE CHANGESCN_BASE
SYS V_$FILESPACE_USAGE CHANGESCN_WRAP
SYS V_$FLASHBACK_DATABASE_LJG JLDEST_FLASHBACK_SCN
SYS V_$KCCDI DICKP_SCN
SYS V_$KCCDI DICUR_SCN
SYS V_$KCCDI DISSC_SCN
SYS V_$KCCDI DISCN
SYS V_$KCCFE FECRC_SCN
SYS V_$KCCFE FEJNC_SCN
SYS V_$KCCFE FEPLG_SCN
SYS V_$LJGMNR_CJNTENTS CSCN
SYS V_$LJGMNR_CJNTENTS SAFE_RESUME_SCN
SYS V_$LJGMNR_CJNTENTS SCN
SYS V_$LJGMNR_CJNTENTS START_SCN
SYS V_$LJGMNR_CJNTENTS CJMMIT_SCN
SYS V_$LJGMNR_DICTIJNARY RESET_SCN_TIME
SYS V_$LJGMNR_DICTIJNARY DB_TXN_SCN
SYS V_$LJGMNR_DICTIJNARY RESET_SCN
SYS V_$LJGMNR_DICTIJNARY DICTIJNARY_SCN
SYS V_$LJGMNR_LJGFILE LJW_SCNBAS
SYS V_$LJGMNR_LJGFILE RESET_SCNWRP
SYS V_$LJGMNR_LJGFILE RESET_SCNBAS
SYS V_$LJGMNR_LJGFILE RESET_SCN_TIME
SYS V_$LJGMNR_LJGFILE NEXT_SCNWRP
SYS V_$LJGMNR_LJGFILE NEXT_SCNBAS
SYS V_$LJGMNR_LJGFILE LJW_SCNWRP
SYS V_$LJGMNR_LJGS NEXT_SCN
SYS V_$LJGMNR_LJGS LJW_SCN
SYS V_$LJGMNR_LJGS RESET_SCN_TIME
SYS V_$LJGMNR_LJGS RESET_SCN
SYS V_$LJGMNR_PARAMETERS START_SCN
SYS V_$LJGMNR_PARAMETERS END_SCN
SYS V_$LJGMNR_PARAMETERS REQUIRED_START_SCN
SYS V_$LJGMNR_SESSIJN END_SCN
SYS V_$LJGMNR_SESSIJN READ_SCN
SYS V_$LJGMNR_SESSIJN RESET_SCN
SYS V_$LJGMNR_SESSIJN LJW_MARK_SCN
SYS V_$LJGMNR_SESSIJN CJNSUMED_SCN
SYS V_$LJGMNR_SESSIJN PRJCESSED_SCN
SYS V_$LJGMNR_SESSIJN START_SCN
SYS V_$LJGMNR_SESSIJN SPILL_SCN
SYS V_$LJGMNR_SESSIJN PREPARED_SCN
...
And MANY MJRE..
Use oI an SCN in SQL-driven Ilashback queries isn`t much diIIerent than using a TIMESTAMP.
Just plug in your SCN number.
SELECT
FRJM flash_test
AS JF SCN 224900;

SELECT
FRJM flash_test
VERSIJNS BETWEEN SCN 2249600 AND 2249659;
You can also play around with converting Irom a TIMESTAMP to an SCN and an SCN to a
TIMESTAMP. These come in handy iI you want to standardize on a particular method or help
you in pinpointing which is better to use during recovery scenarios.
Convert SCN to TIMESTAMP.
SQL SELECT SCN_TJ_TIMESTAMP(2249659) FRJM dual;

SCN_TJ_TIMESTAMP(2249659)
------------------------------------------------
-JUN-09 0.30.09.000000000 AM

Convert TIMESTAMP to SCN
SQL SELECT TIMESTAMP_TJ_SCN('-JUN-09 0.30.09.000000000 AM') FRJM dual;

TIMESTAMP_TJ_SCN('-JUN-090.30.09.000000000AM')
-------------------------------------------------
2249659
Probably worth mentioning, since Ilashback query isn't "supposed" to work across database
bounces, is the Iollowing query to extract the initial TIMESTAMP and SCN Irom instance
startup.
SQL SELECT
TJ_TIMESTAMP(TJ_CHAR(startup_time,'YYYYMMDDHH24MISS'),'YYYYMMDDHH24MISS')
TIMESTAMP,
2
TIMESTAMP_TJ_SCN(TJ_TIMESTAMP(TJ_CHAR(startup_time,'YYYYMMDDHH24MISS'),'YYYYM
MDDHH24MISS') ) SCN
3 FRJM v$instance;

TIMESTAMP SCN
----------------------------------- ----------
-JUN-09 06.49.04.000000000 AM 223632
OItentimes, but not always, Ilashback queries that attempt to SELECT data beIore these times
will generate the Iollowing error:
SQL SELECT
2 FRJM flash_test
3 AS JF SCN 000000;
FRJM flash_test

ERRJR at line 2:
JRA-0466: unable to read data - table definition has changed
Determining to use an SCN or TIMESTAMP Ior Ilashback queries seems to be a matter oI
comIort. I think it really comes down to whether you`re comIortable working with times or
numbers. Worthy to note, since there are various human Iactors that come into play when
discussing time (TIMEZONE and client-side wall clock or system time to name just a couple) it
may be better to always convert to a real database server side TIMESTAMP and then an SCN.
Moreover, this sounds much more proIessional, can help obIuscate the issue, and make you
sound like you`re remaining technical; always a plus!


A)Determ|ne current SCN of 1he database

?ou can query from v$uA1A8ASL or uslng CL1_S?S1LM_CPAnCL_nuM8L8 procedure of
u8MS_lLASP8ACk package

1)SL SLLLC1 D8MS_ILASn8ACkGL1_SS1LM_CnANGL_NUM8Lk() IkCM DUAL
u8MS_lLASP8ACkCL1_S?S1LM_CPAnCL_nuM8L8()

1033638

2)SL SLLLC1 CUkkLN1_SCN IkCM V5DA1A8ASL
Cu88Ln1_SCn

1033668

8)Determ|ne current t|mestamp va|ue

SL SLLLC1 SS1IMLS1AM IkCM DUAL
S?S1lMLS1AM

14MA?08 110008374107 M 0400

C)Convert SCN to 1|mestamp

SL SLLLC1 SCN_1C_1IMLS1AM(10S3639) IkCM DUAL
SCn_1C_1lMLS1AM(1033639)

14MA?08 103213000000000 M

D)Convert 1|mestamp to SCN

SL SLLLC1 1IMLS1AM_1C_SCN(14MA08 110008374107 M) IkCM DUAL
1lMLS1AM_1C_SCn(14MA?08110008374107M)

1034316

You might also like