Retrieve old versions of procedures with Flashback Query

rewind

Each stored procedure (or trigger, package etc.) is reflected in DBA_SOURCE, of course. Now imagine, you modified or even dropped a procedure without having the code available to recreate it properly. In this case, you may save the day by using Flashback Query (possible since 9i) against DBA_SOURCE, like in this example:

SQL> select text from dba_source where name='UPD_SALES' order by line;

TEXT
--------------------------------------------------------------------------------
procedure upd_sales as
begin
for i in 1..10000 loop
update sales set amount_sold=amount_sold*1 where rownum<2;
commit;
end loop;
end;

7 rows selected.

SQL> drop procedure upd_sales;

Procedure dropped.

SQL> connect / as sysdba
Connected.

SQL> select text from dba_source 
     as of timestamp systimestamp - interval '5' minute  
     where name='UPD_SALES' order by line;

TEXT
--------------------------------------------------------------------------------
procedure upd_sales as
begin
for i in 1..10000 loop
update sales set amount_sold=amount_sold*1 where rownum<2;
commit;
end loop;
end;

7 rows selected.

I could spool the output from above into a text file and use it afterwards to recreate my procedure as it was before 5 minutes! By the way, I know that the commit inside the loop is not a good idea here 🙂

  1. #1 von coskan am Juni 3, 2009 - 15:05

    Never thought to use it that way nice one. Thank you UWE

  2. #2 von Kamran Agayev A. am September 4, 2009 - 12:39

    Nice solution 🙂

  3. #3 von Junko Greto am Dezember 20, 2010 - 03:02

    I don’t unremarkably comment but I gotta admit appreciate it for the post on this special one : D.

  4. #4 von wii guitar am Juni 29, 2011 - 21:13

    would love to forever get updated great site ! .

  5. #5 von Edgar am Februar 23, 2013 - 22:02

    very cool solution, thanks for sharing this!!

  6. #6 von Joshua am April 15, 2013 - 23:21

    Can we use the same query to retrieve the a package that was compiled 10 days ago? If yes, can you please let me know how?

  7. #7 von Uwe Hesse am April 16, 2013 - 08:42

    Joshua, that is a bit unlikely, because the flashback query relies on before images in the undo tablespace – which are probably overwritten after 10 days. Just replace interval ‚5‘ minute with interval ’10‘ day to try.

  8. #8 von onzi snoja am November 28, 2014 - 19:37

    saved 10 hrs of restore and etc.. – many 10x ;))

  9. #9 von Sebastián am Januar 26, 2015 - 21:17

    Thanks a lot a lot a lot, I have saved a lot time od restore

  10. #10 von tenzorok am Februar 13, 2015 - 14:09

    Thank you for the post, You just made the hero of the day 🙂

  11. #11 von Siyad Saleem am Februar 4, 2016 - 13:09

    Thank you, This one really helps

  12. #12 von fouedgray am Februar 5, 2016 - 23:16

    Very nice hint, thanks

  13. #13 von Prathap am Juli 25, 2016 - 14:29

    This was really helped me in terms of Effort and Time. Thanks alot 🙂

  14. #14 von Ra'fat Haj Ali am August 23, 2016 - 15:39

    Many Thanks, its helped me greatly, its save work for month or more
    really its great

  15. #15 von Jon wall am November 2, 2017 - 15:16

    Thanks, which flashback settings do we need to be on in the database for this to work?

  16. #16 von Uwe Hesse am November 3, 2017 - 14:04

    Jon, this is always on from 9i onwards. It doesn’t require flashback logs, so V$DATABASE.FLASHBACK_ON is not relevant for this feature. The parameter UNDO_RETENTION can be used to configure how long to preserve the undo information that feature depends on.

  1. How to configure FLASHBACK in #Oracle | Uwe Hesse

Hinterlasse einen Kommentar

Diese Seite verwendet Akismet, um Spam zu reduzieren. Erfahre, wie deine Kommentardaten verarbeitet werden..