Home » SQL & PL/SQL » SQL & PL/SQL » Recover rows after delete and commit (Oracle 11G)
Recover rows after delete and commit [message #623007] Wed, 03 September 2014 05:28 Go to next message
hemant_khandare
Messages: 72
Registered: November 2011
Member
hi Experts,

How can i recover rows after delete and commit.

suppose I have emp table and deleted 4 rows and then commit.

How can i restore my rows which I lost accidently.



Thanks in Advance....
Re: Recover rows after delete and commit [message #623012 is a reply to message #623007] Wed, 03 September 2014 06:02 Go to previous messageGo to next message
John Watson
Messages: 8929
Registered: January 2010
Location: Global Village
Senior Member
orclz>
orclz> delete from emp;

14 rows deleted.

orclz> commit;

Commit complete.

orclz> insert into emp select *  from emp as of timestamp (sysdate - 10/1440);

14 rows created.

orclz> commit;

Commit complete.

orclz>


Re: Recover rows after delete and commit [message #623017 is a reply to message #623012] Wed, 03 September 2014 06:43 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Another way:
SQL> delete from emp;

14 rows deleted.

SQL> commit;

Commit complete.

SQL> alter table emp enable row movement;

Table altered.

SQL> flashback table emp to timestamp (sysdate - 10/1440);

Flashback complete.

SQL> select count(*) from emp;
  COUNT(*)
----------
        14

1 row selected.

Re: Recover rows after delete and commit [message #623024 is a reply to message #623007] Wed, 03 September 2014 07:34 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
One way to retain/log the deleted rows.

SQL> create table emp1 as select * from emp;

Table created.

SQL> set serveroutput on;
SQL> DECLARE
  2      TYPE emp_typ
  3        IS TABLE OF emp1%ROWTYPE;
  4      emp_info EMP_TYP;
  5      l_cnt    NUMBER;
  6  BEGIN
  7      DELETE emp1
  8      returning empno, ename, job, mgr, hiredate, sal, comm, deptno bulk collect
  9      INTO
 10      emp_info;
 11
 12      COMMIT;
 13
 14      SELECT Count(*)
 15      INTO   l_cnt
 16      FROM   emp1;
 17
 18      dbms_output.Put_line('l_cnt after deletion ='
 19                           ||l_cnt);
 20
 21      FOR i IN emp_info.first..emp_info.last LOOP
 22          INSERT INTO emp1
 23          VALUES     (Emp_info(i).empno,
 24                      Emp_info(i).ename,
 25                      Emp_info(i).job,
 26                      Emp_info(i).mgr,
 27                      Emp_info(i).hiredate,
 28                      Emp_info(i).sal,
 29                      Emp_info(i).comm,
 30                      Emp_info(i).deptno);
 31      END LOOP;
 32
 33      commit;
 34
 35      SELECT Count(*)
 36      INTO   l_cnt
 37      FROM   emp1;
 38
 39      dbms_output.Put_line('l_cnt after returning the deleted rows ='
 40                           ||l_cnt);
 41  END;
 42  /
l_cnt after deletion =0
l_cnt after returning the deleted rows =14

PL/SQL procedure successfully completed.

SQL>



Regards,
Lalit
Re: Recover rows after delete and commit [message #623026 is a reply to message #623024] Wed, 03 September 2014 08:28 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
 21      FOR i IN emp_info.first..emp_info.last LOOP
 22          INSERT INTO emp1
 23          VALUES     (Emp_info(i).empno,
 24                      Emp_info(i).ename,
 25                      Emp_info(i).job,
 26                      Emp_info(i).mgr,
 27                      Emp_info(i).hiredate,
 28                      Emp_info(i).sal,
 29                      Emp_info(i).comm,
 30                      Emp_info(i).deptno);
 31      END LOOP;


Better use FORALL here and avoid the loop.

Re: Recover rows after delete and commit [message #623028 is a reply to message #623026] Wed, 03 September 2014 08:43 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
True. Thanks for pointing it out.
Re: Recover rows after delete and commit [message #623144 is a reply to message #623028] Fri, 05 September 2014 03:48 Go to previous message
hemant_khandare
Messages: 72
Registered: November 2011
Member
Thanks a lot ro all of u....
Previous Topic: Update trigger (merged 2)
Next Topic: need to find exception line no inside procedure of 1000 lines.
Goto Forum:
  


Current Time: Fri Apr 19 12:02:38 CDT 2024