Oracle's flashback query (SQL-driven) makes use of both timestamp and SCN--but which should you use? the answer is not always clear but ill investigate both here to help you grapple with your next decision. TIMESTamp is an extension oI the date datatype, which stores all the inIormation that the date (date) datatype stores (month, day, year, hour, minute, second) also includes fractional seconds.
Oracle's flashback query (SQL-driven) makes use of both timestamp and SCN--but which should you use? the answer is not always clear but ill investigate both here to help you grapple with your next decision. TIMESTamp is an extension oI the date datatype, which stores all the inIormation that the date (date) datatype stores (month, day, year, hour, minute, second) also includes fractional seconds.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online from Scribd
Oracle's flashback query (SQL-driven) makes use of both timestamp and SCN--but which should you use? the answer is not always clear but ill investigate both here to help you grapple with your next decision. TIMESTamp is an extension oI the date datatype, which stores all the inIormation that the date (date) datatype stores (month, day, year, hour, minute, second) also includes fractional seconds.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online from Scribd
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 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%'
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
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