一、将指定的时间(timestamp)转换为scn
格式1:linux下 sqlplus scott/tiger
1. 查询当前系统时间格式
SQL> select sysdate from dual;
SYSDATE
---------
03-NOV-17
2. 查询当前时间和scn
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') Time,
timestamp_to_scn(sysdate) SCN from dual;
TIME SCN
------------------- ----------
2017-11-03 21:20:48 939576
3. 转换为scn
SQL> select timestamp_to_scn('03-NOV-17 09:20:48.000000000 PM') SCN from dual;
SCN
----------
939576
----------------------------------------------------------------------------------------------------------------------------------
格式2:windows cmd下 sqlplus scott/tiger
1. 查询当前系统时间格式
SQL> select sysdate from dual;
SYSDATE
--------------
16-1月 -18
2. 查询当前时间和scn
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') Time,
timestamp_to_scn(sysdate) SCN from dual;
TIME SCN
------------------- ----------
2018-01-16 18:46:50 2547225
3. 转换为scn
SQL> select timestamp_to_scn('16-1月 -18 06:46:50.000000000 下午') SCN from dual;
SCN
----------
2547225
----------------------------------------------------------------------------------------------------------------------------------
格式3:PL/SQL下 命令窗口
1. 查询当前系统时间格式
SQL> select sysdate from dual;
SYSDATE
-----------
2018/1/16 1
2. 查询当前时间和scn
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') Time,
timestamp_to_scn(sysdate) SCN from dual;
TIME SCN
------------------- ----------
2018-01-16 18:49:05 2547282
3. 转换为scn
SQL> select timestamp_to_scn('16-1月 -18 06:49:05.000000000 下午') SCN from dual;
SCN
----------
2547283
注:命令窗口下的scn有差距 2547282 2547283
----------------------------------------------------------------------------------------------------------------------------------
格式4:PL/SQL下 SQL窗口
1. 查询当前系统时间格式
select sysdate from dual;
2018/1/16 18:51:03
2. 查询当前时间和scn
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') Time,
timestamp_to_scn(sysdate) SCN from dual;
2018-01-16 18:51:21 2547469
3. 转换为scn
select timestamp_to_scn('16-1月 -18 06:51:21.000000000 下午') SCN from dual;
2547470
注:SQL窗口下的scn有差距 2547469 2547470
----------------------------------------------------------------------------------------------------------------------------------
二、scn转换为timestamp
SQL> select scn_to_timestamp(939576) scn from dual;
SCN
---------------------------------------------------------------------------
03-NOV-17 09.20.48.000000000 PM
三、通过scn或timestamp查询过去某个时间点上的数据
1. 查询sal
SQL> select sal from emp where empno=7839;
SAL
----------
6000
2. 查询当前时间和scn
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') Time,
timestamp_to_scn(sysdate) SCN from dual; 2
TIME SCN
------------------- ----------
2017-11-03 21:55:29 941738
3. 更新sal
update emp set sal=7000 where empno=7839;
4. 提交
commit;
5. 查询commit后的saL
SQL> select sal from emp where empno=7839;
SAL
----------
7000
6. 根据scn查询没有commit前的sal
SQL> select sal from emp as of scn 941738 where empno=7839;
SAL
----------
6000
7. 根据timestamp没有commit前的sal
SQL> select sal from emp as of timestamp to_timestamp('2017-11-03 21:55:29', 'yyyy-mm-dd hh24:mi:ss') where empno=7839;
SAL
----------
6000
---end----