Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
40
Добавлен:
16.04.2013
Размер:
4.96 Mб
Скачать

Using Flashback Transaction Query

VERSIONS BETWEEN TIMESTAMP

TO_TIMESTAMP('2003-07-18 14:00:00', 'YYYY-MM-DD HH24:MI:SS')

AND TO_TIMESTAMP('2003-07-18 17:00:00', 'YYYY-MM-DD HH24:MI:SS')

WHERE name = 'JOE';

Pseudocolumn VERSIONS_XID provides a unique identifier for the transaction that put the data in that state. You can use this value in connection with a Flashback Transaction Query to locate metadata about this transaction in the FLASHBACK_ TRANSACTION_QUERY view, including the SQL required to undo the row change and the user responsible for the change – see "Using Flashback Transaction Query" on page 15-12.

See Also: Oracle Database SQL Reference for information on the

Flashback Version Query pseudocolumns and the syntax of the

VERSIONS clause

Using Flashback Transaction Query

A Flashback Transaction Query is a query on the view FLASHBACK_ TRANSACTION_QUERY. You use a Flashback Transaction Query to obtain transaction information, including SQL code that you can use to undo each of the changes made by the transaction.

See Also: Oracle Database Backup and Recovery Advanced User's Guide. and Oracle Database Administrator's Guide for information on how a DBA can use the Flashback Table feature to restore an entire table, rather than individual rows

As an example, the following statement queries the FLASHBACK_TRANSACTION_ QUERY view for transaction information, including the transaction ID, the operation, the operation start and end SCNs, the user responsible for the operation, and the SQL code to undo the operation:

SELECT xid, operation, start_scn,commit_scn, logon_user, undo_sql

FROM flashback_transaction_query

WHERE xid = HEXTORAW('000200030000002D');

As another example, the following query uses a Flashback Version Query as a subquery to associate each row version with the LOGON_USER responsible for the row data change.

SELECT xid, logon_user FROM flashback_transaction_query

WHERE xid IN (SELECT versions_xid FROM employee VERSIONS BETWEEN TIMESTAMP

15-12 Oracle Database Application Developer's Guide - Fundamentals

Using Flashback Transaction Query

TO_TIMESTAMP('2003-07-18 14:00:00', 'YYYY-MM-DD HH24:MI:SS') AND

TO_TIMESTAMP('2003-07-18 17:00:00', 'YYYY-MM-DD HH24:MI:SS'));

Flashback Transaction Query and Flashback Version Query: Example

This example demonstrates the use of a Flashback Transaction Query in conjunction with a Flashback Version Query. The example assumes simple variations of the employee and departments tables in the sample hr schema.

In this example, a DBA carries out the following series of actions in SQL*Plus:

connect hr/hr CREATE TABLE emp

(empno number primary key, empname varchar2(16), salary number); INSERT INTO emp VALUES (111, 'Mike', 555);

COMMIT;

CREATE TABLE dept (deptno number, deptname varchar2(32));

INSERT INTO dept VALUES (10, 'Accounting');

COMMIT;

At this point, emp and dept have one row each. In terms of row versions, each table has one version of one row. Next, suppose that an erroneous transaction deletes employee id 111 from table emp:

UPDATE emp SET salary = salary + 100 where empno = 111;

INSERT INTO dept VALUES (20, 'Finance');

DELETE FROM emp WHERE empno = 111;

COMMIT;

Subsequently, a new transaction reinserts employee id 111 with a new employee name into the emp table.

INSERT INTO emp VALUES (111, 'Tom', 777);

UPDATE emp SET salary = salary + 100 WHERE empno = 111;

UPDATE emp SET salary = salary + 50 WHERE empno = 111;

COMMIT;

At this point, the DBA detects the application error and needs to diagnose the problem. The DBA issues the following query to retrieve versions of the rows in the emp table that correspond to empno 111. The query uses Flashback Version Query pseudocolumns.

connect dba_name/password

SELECT versions_xid XID, versions_startscn START_SCN, versions_endscn END_SCN, versions_operation OPERATION,

Using Flashback Features 15-13

Using Flashback Transaction Query

empname, salary FROM hr.emp

VERSIONS BETWEEN SCN MINVALUE AND MAXVALUE where empno = 111;

XID

START_SCN

END_SCN

OPERATION

EMPNAME

SALARY

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

----------

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

----------

----------

0004000700000058

113855

 

I

Tom

927

000200030000002D

113564

 

D

Mike

555

000200030000002E

112670

113564

I

Mike

555

3 rows selected

 

 

 

 

 

The results table reads chronologically, from bottom to top. The third row corresponds to the version of the row in emp that was originally inserted in the table when the table was created. The second row corresponds to the row in emp that was deleted by the erroneous transaction. The first row corresponds to the version of the row in emp that was reinserted with a new employee name.

The DBA identifies transaction 000200030000002D as the erroneous transaction and issues the following Flashback Transaction Query to audit all changes made by this transaction:

SELECT xid, start_scn START, commit_scn COMMIT, operation OP, logon_user USER,

undo_sql FROM flashback_transaction_query

WHERE xid = HEXTORAW('000200030000002D');

XID

START

COMMIT

OP

USER

UNDO_SQL

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

-----

------

--

----

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

000200030000002D

195243

195244

DELETE

HR

insert into "HR"."EMP"

("EMPNO","EMPNAME","SALARY") values ('111','Mike','655');

000200030000002D

195243

195244

INSERT

HR

delete from "HR"."DEPT"

where ROWID = 'AAAKD4AABAAAJ3BAAB';

 

 

000200030000002D

195243

195244

UPDATE

HR

update "HR"."EMP"

set "SALARY" = '555' where ROWID = 'AAAKD2AABAAAJ29AAA';

000200030000002D 195243 113565 BEGIN HR

4 rows selected

The rightmost column (undo_sql) contains the SQL code that will undo the corresponding change operation. The DBA can execute this code to undo the changes made by that transaction. The USER column (logon_user) shows the user responsible for the transaction.

15-14 Oracle Database Application Developer's Guide - Fundamentals

Соседние файлы в папке Oracle 10g