{"id":203,"date":"2008-12-09T14:50:21","date_gmt":"2008-12-09T13:50:21","guid":{"rendered":"http:\/\/www.askthegerman.com\/?p=203"},"modified":"2023-02-13T07:36:24","modified_gmt":"2023-02-13T06:36:24","slug":"flashback-queries","status":"publish","type":"post","link":"https:\/\/www.askthegerman.com\/?p=203","title":{"rendered":"Flashback Queries"},"content":{"rendered":"<p>Working with the Oracle Flashback option we can restore the old data using flashback queries. Introduced in Oracle Version 9.2 the &#8220;AS OF&#8221; clause can be used to:<\/p>\n<ul>\n<li>find the old data<\/li>\n<li>restore the old data<\/li>\n<\/ul>\n<p>Mistakenly set the salary of Manager King to a wrong value:<br \/>\n<em><br \/>\nSQL&gt; update emp set sal=4500 where ename=&#8217;KING&#8217;;<br \/>\n1 row updated.<br \/>\nSQL&gt; commit;<br \/>\nCommit complete.<\/em><\/p>\n<p><em>SQL&gt; select ename, sal from emp where ename=&#8217;KING&#8217;;<br \/>\nENAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SAL<br \/>\n&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<br \/>\nKING&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4500<\/em><\/p>\n<p>We can query the old value again using the &#8220;AS OF&#8221; clause and specifying a time interval:<\/p>\n<p>Issuing this query will tell us the correct value as it was existing 15 minutes before.<em><\/em><\/p>\n<p><em>SELECT ename, sal FROM emp<br \/>\nAS OF TIMESTAMP (SYSTIMESTAMP &#8211; INTERVAL &#8217;15&#8217; MINUTE)<br \/>\nWHERE ename = &#8216;KING&#8217;;<\/em><\/p>\n<p><em>ENAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SAL<br \/>\n&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<br \/>\nKING&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5000<\/em><\/p>\n<p>Now we can use flashback query as a sub query in an update statement to restore back to the old value. The command is as follows:<\/p>\n<p><em>update emp<br \/>\nset sal = (<br \/>\nSELECT sal FROM emp<br \/>\nAS OF TIMESTAMP (SYSTIMESTAMP &#8211; INTERVAL &#8217;15&#8217; MINUTE) where ename=&#8217;KING&#8217;)<br \/>\nwhere ename=&#8217;KING&#8217;;<\/em><\/p>\n<p>In case you are going to use this for testing purposes, you might find it usefully to record the referencing SCN and use flashback query in combination with the recorded SCN.<\/p>\n<p><em>SQL&gt; select dbms_flashback.get_system_change_number from dual;<br \/>\nGET_SYSTEM_CHANGE_NUMBER<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4312233<\/em><\/p>\n<p>Now changing a value on purpose:<\/p>\n<p><em>SQL&gt; update emp set sal=4300 where ename=&#8217;KING&#8217;;<br \/>\n1 row updated.<\/em><\/p>\n<p><em>SQL&gt; commit;<br \/>\nCommit complete.<\/em><\/p>\n<p><em>SQL&gt; select ename, sal from emp where ename=&#8217;KING&#8217;;<br \/>\nENAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SAL<br \/>\n&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<br \/>\nKING&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4300<br \/>\n<\/em><br \/>\nThen you might not know the previous salary of MILLER but you do know the SCN when it refered to the old value and the original value can be updated and restored with flashback query by using the SCN noted prior to the update statement.<\/p>\n<p>Here is the required query:<\/p>\n<p><em>SELECT ename, sal FROM emp<br \/>\nAS OF SCN 4312233<br \/>\nwhere ename=&#8217;KING&#8217;;<\/em><\/p>\n<p><em>ENAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SAL<br \/>\n&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<br \/>\nKING&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4300<\/em><\/p>\n<p>And you can restore the old value by updating the row or better you directly restore the old value using the following update with flashback sub query:<\/p>\n<p><em>update emp<br \/>\nset sal = (<br \/>\nSELECT sal FROM emp<br \/>\nAS OF SCN 4312233 where ename=&#8217;KING&#8217;)<br \/>\nwhere ename=&#8217;KING&#8217;;<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Working with the Oracle Flashback option we can restore the old data using flashback queries. Introduced in Oracle Version 9.2 the &#8220;AS OF&#8221; clause can be used to: find the old data restore the old data Mistakenly set the salary of Manager King to a wrong value: SQL&gt; update emp set sal=4500 where ename=&#8217;KING&#8217;; 1 [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5,31],"tags":[191,189,190,257,192,42],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry","category-database","category-oracle","category-tipsntricks","tag-as-of","tag-flashback-option","tag-flashback-query","tag-oracle","tag-select","tag-update"],"_links":{"self":[{"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=203"}],"version-history":[{"count":7,"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=\/wp\/v2\/posts\/203\/revisions"}],"predecessor-version":[{"id":351,"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=\/wp\/v2\/posts\/203\/revisions\/351"}],"wp:attachment":[{"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}