Home » RDBMS Server » Server Administration » Delete, Insert and rollback to before delete if insert fails
Delete, Insert and rollback to before delete if insert fails [message #122067] Thu, 02 June 2005 12:33 Go to next message
Ricky_newbie
Messages: 12
Registered: May 2005
Junior Member
All,
I have a problem here with writing program.
I have a delete statement and inserting records into the same table. I have a situation where insert fails with Duplicate values. But for now what i need is if there is any way, i can program to rollback to before delete statement and goto next step. This is my example;

delete from emp where deptno = '10'

insert into emp(deptno)
select deptno from dept

If insert fails here
Rollback to before delete
commit;
else commit;

and goto next step

Help needed please.

Thanks,
Rick
Re: Delete, Insert and rollback to before delete if insert fails [message #122069 is a reply to message #122067] Thu, 02 June 2005 13:07 Go to previous messageGo to next message
girish.rohini
Messages: 744
Registered: April 2005
Location: Delhi (India)
Senior Member
use savepoints in your procedure.
Re: Delete, Insert and rollback to before delete if insert fails [message #122074 is a reply to message #122069] Thu, 02 June 2005 13:37 Go to previous messageGo to next message
Ricky_newbie
Messages: 12
Registered: May 2005
Junior Member
Thanks Girish,
I under stand to use savepoint. How would i say my program if Insert fails? I am newbie to sql programming. Can you please give me details?
Re: Delete, Insert and rollback to before delete if insert fails [message #122080 is a reply to message #122067] Thu, 02 June 2005 14:20 Go to previous messageGo to next message
Ricky_newbie
Messages: 12
Registered: May 2005
Junior Member
Girish,
I have an idea of doing this..Let me know if i am right here.


Savepoint emp_sp
delete from emp where deptno = '10'
insert into emp(deptno)
select deptno from dept
commit;
when SQLCODE
then
rollback to emp_sp
commit;

Please let me know if this would work.
Re: Delete, Insert and rollback to before delete if insert fails [message #122119 is a reply to message #122080] Fri, 03 June 2005 00:34 Go to previous messageGo to next message
girish.rohini
Messages: 744
Registered: April 2005
Location: Delhi (India)
Senior Member
Well in first reply I mistook that you want to commit after delete in case insertion fails.

If you want to rollback all your work if insertion fails after deletion, u can simply use rollback.
Do clarify me if I am wrong in understanding scenario:
U want the following taks:
0. Start of transaction
1. delete from emp where deptno = '10'

2. insert into emp(deptno) select deptno from dept

3. commit if no error else
when error rollback to 0.

If this is the case, simply check for exception and issue rollback in the when error clause at step 3.

But in case u want to rollback upto stage after step 1, then use savepoint there before step 2 and after step 1.
Declare it like savepoint s.
And then in when error clause give rollback to s & then commit.
Re: Delete, Insert and rollback to before delete if insert fails [message #122630 is a reply to message #122119] Tue, 07 June 2005 12:17 Go to previous messageGo to next message
Ricky_newbie
Messages: 12
Registered: May 2005
Junior Member
Girish,
Thanks for the reply. I havent got chance to open forum in last 3 days. About my issue, resolved using exceptions.
Thanks a lot for your help.
Re: Delete, Insert and rollback to before delete if insert fails [message #122683 is a reply to message #122630] Wed, 08 June 2005 00:17 Go to previous message
girish.rohini
Messages: 744
Registered: April 2005
Location: Delhi (India)
Senior Member
cool!!!! tats good that it worked for u.....
Previous Topic: Oracle Migration
Next Topic: vb.net connection with oracle
Goto Forum:
  


Current Time: Fri Jan 10 07:35:51 CST 2025