天天看点

Oracle10g中SCN与TimeStamp的相互转换

Oracle10g中SCN与TimeStamp的相互转换



作为对于闪回操作(flashback)的一个增强,Oracle10g提供了函数对于SCN和时间戳进行相互转换 



首先通过dbms_flashback.get_system_change_number 可以获得系统当前的SCN值:





SQL> col scn for 9999999999999

SQL> select dbms_flashback.get_system_change_number scn from dual; 



SCN

--------------

8908390522972 





通过scn_to_timestamp函数可以将SCN转换为时间戳:





SQL> select scn_to_timestamp(8908390522972) scn from dual; 





SCN

---------------------------------------------------------------------------

05-JAN-07 10.56.30.000000000 AM 





再通过timestamp_to_scn可以将时间戳转换为SCN: 



SQL> select timestamp_to_scn(scn_to_timestamp(8908390522972)) scn from dual; 

SCN

--------------

8908390522972





通过这两个函数,最终Oracle将SCN和时间的关系建立起来,在Oracle10g之前,是没有办法通过函数转换得到SCN和时间的对应关系的,一般可以通过logmnr分析日志获得。 



但是这种转换要依赖于数据库内部的数据记录,对于久远的SCN则不能转换,请看以下举例:





SQL> select min(FIRST_CHANGE#) scn,max(FIRST_CHANGE#) scn from v$archived_log; 



SCN SCN

------------------ ------------------

8907349093953 8908393582271 



SQL> select scn_to_timestamp(8907349093953) scn from dual;

select scn_to_timestamp(8907349093953) scn from dual

*

ERROR at line 1:

ORA-08181: specified number is not a valid system change number

ORA-06512: at /"SYS.SCN_TO_TIMESTAMP/", line 1

ORA-06512: at line 1





SQL> select scn_to_timestamp(8908393582271) scn from dual; 网管下载dl.bitscn.com 



SCN

---------------------------------------------------------------------------

05-JAN-07 11.45.50.000000000 AM 





原文地址: http://www.eygle.com/archives/2007/01/scntimestamp_function.html 

【转自www.bitsCN.com】