Monday, June 14, 2010

Oracle Flashback Query

Here is a small, usable example of Oracle's Flashback Query feature. This can be a lifesaver when you forget a where clause when updating/deleting records. This example also showcases the TO_TIMESTAMP function, which is required by the flashback syntax:

CREATE TABLE test (test VARCHAR2(20));
INSERT INTO test (test) VALUES ('good value'); 
-- Inserted the value on December 23rd, at 10:20 AM

UPDATE test SET test = ‘bad value’; 
-- I did this at 10:22, OOPS!

SELECT test FROM test; 
-- Returns ‘bad value’

SELECT test FROM test AS OF TIMESTAMP(TO_TIMESTAMP(‘2009-12-23 10:21:00’, ‘YYYY-MM-DD HH24:MI:SS’)); 
-- Returns ‘good value’

Simple, quick and easy! 

No comments:

Post a Comment