Páginas

sexta-feira, 1 de março de 2013

Identificando último DML em uma tabela Oracle


Identificando última alteração DML (aproximada) em uma tabela:

SQL> select max(ora_rowscn), scn_to_timestamp(max(ora_rowscn)) from sys.teste;

MAX(ORA_ROWSCN) SCN_TO_TIMESTAMP(MAX(ORA_ROWSCN))
--------------- ---------------------------------------------------------------------------
      849681860 01-MAR-13 11.31.38.000000000 AM

SQL> update teste set a=0;

1 row updated.

SQL> commit;

Commit complete.

SQL> select max(ora_rowscn), scn_to_timestamp(max(ora_rowscn)) from sys.teste;

MAX(ORA_ROWSCN) SCN_TO_TIMESTAMP(MAX(ORA_ROWSCN))
--------------- ---------------------------------------------------------------------------
      849682200 01-MAR-13 11.32.13.000000000 AM

* Para tabelas grandes, é mais eficiente buscar a última linha da tabela (se possível pela PK) e depois disso pegar o ORA_ROWSCN dela:

select max(scn_to_timestamp(ora_rowscn)) from tabela where campo_pk = (select max(campo_pk) from tabela);

Referência: Ask Tom (http://asktom.oracle.com/pls/asktom/fp=100:11:0::::P11_QUESTION_ID:1590655700346557237)

Mais informações: http://docs.oracle.com/cd/E14072_01/server.112/e10592/pseudocolumns007.htm