SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Rollbck Transaction
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kalyan.cse05
Yak Posting Veteran

India
68 Posts

Posted - 05/18/2012 :  07:00:39  Show Profile  Reply with Quote
I have an Insert script which insert value into two table. I need to put the entire insert script under a single transaction and if the the script fails to insert data in any of the table then the whole transaction should be rollbacked.

Can any one please help.
I am trying with the below:

Begin tran
Begin
Insert into A (a) values (1)
Insert into B (b) values (2)
Insert into B (b) values (3) --suppose fails in this step then the entire traction should be rolled back
Insert into B (b) values (4)
Insert into B (b) values (5)
END
IF (@@ERROR<>0)
Rollback Tran

Else
Commit Tran


But did not work. :(

kalyan Ashis Dey

kalyan.cse05
Yak Posting Veteran

India
68 Posts

Posted - 05/18/2012 :  07:08:30  Show Profile  Reply with Quote
I understand one thing now @@ERROR always return the status of the last insert statement. So will it be work if i put the entire thing under a try catch block?

kalyan Ashis Dey
Go to Top of Page

kalyan.cse05
Yak Posting Veteran

India
68 Posts

Posted - 05/18/2012 :  07:58:23  Show Profile  Reply with Quote
Got my answer :)
need to put the whole thing in try block and rollback section in catch block. i.e:
begin try
Begin tran
Begin
Insert into A (a) values (1)
Insert into B (b) values (2)
Insert into B (b) values (3) --suppose fails in this step then the entire traction should be rolled back
Insert into B (b) values (4)
Insert into B (b) values (5)
commit tran
END
end try
begin catch
Rollback Tran
end catch



kalyan Ashis Dey
Go to Top of Page

DonAtWork
Flowing Fount of Yak Knowledge

2111 Posts

Posted - 05/18/2012 :  10:15:30  Show Profile  Reply with Quote
Excellent! You answered your own question! Best way to learn, and saved me typing it all out too









How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

vijays3
Constraint Violating Yak Guru

India
311 Posts

Posted - 05/19/2012 :  06:30:06  Show Profile  Reply with Quote

I tried and It is working fine.It rollback all the records..


Begin tran
Begin

Insert into B (Val) values (2)
Insert into B (Val) values ('vava') --suppose fails in this step then the entire traction should be rolled back
Insert into B (Val) values (4)
Insert into B (Val) values (5)
END
IF (@@ERROR<>0)
Rollback Tran

Else
Commit Tran




Vijay is here to learn something from you guys.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000