该内容来源官网:
http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions161.htm#BABEHBCB
其用法很简单
scn_to_timestamp(number),其中number指的是系统当前生产的SCN
文档中NOTE部分提到一个内容值得注意:
The association between an SCN and a timestamp when the SCN is generated is remembered by the database for a limited period of time. This period is the maximum of the auto-tuned undo retention period, if the database runs in the Automatic Undo Management mode, and the retention times of all flashback archives in the database, but no less than 120 hours. The time for the association to become obsolete elapses only when the database is open. An error is returned if the SCN specified for the argument to SCN_TO_TIMESTAMP
is too old.
这里说到当SCN生成时数据库在有限的期限内保留SCN和TIMESTAMP间的关系,这个期限是由undo最大的保存时间来确定,如果数据库运行了UNDO的自动管理,并且存储的闪回归档不小于120小时,则仅当数据库开启时这个关系会过期,当通过SCN_TO_TIMESTAMP函数查询时,会返回参数太旧的错误提示。
同时文档中提到一个有趣的用法,Oracle提供了一个叫ORA_ROWSCN的虚拟列,当你对某些表进行查询是,通过SCN_TO_NUMBER()参数中指定该虚拟字段,其返回的结果是该表中行最后一次升级的时间。
SQL> select scn_to_timestamp(ORA_ROWSCN) from tbilllog3;
SCN_TO_TIMESTAMP(ORA_ROWSCN)
---------------------------------------------------------------------
05-JAN-15 02.25.28.000000000 PM
05-JAN-15 05.28.56.000000000 PM
其目的主要在于闪回查询,通过时间戳将数据回退到某一个时间点。例子可以看一下官网中的介绍
如果指定的是未来的某个SCN数,则会出现如下错误,
SQL> select scn_to_timestamp(2095591) from dual;
select scn_to_timestamp(2095591) 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