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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Using Transaction Rollback
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

swissivan
Starting Member

30 Posts

Posted - 02/26/2013 :  22:08:47  Show Profile  Reply with Quote
My objective is to update two tables in the same time and make sure the data is in sync.
Real Case : Customer buy a product, system will create a record in ProductTransaction and then update balance of that product in another table ProductBalance.

I would like to know is it using transaction rollback as below is really safe? thanks.

BEGIN TRANSACTION

INSERT INTO ProductTransaction ....

IF @@ERROR <> 0
BEGIN
ROLLBACK

RAISERROR ('Error when insert ProductTransaction.', 16, 1)
RETURN
END

UPDATE ProductBalance ....

IF @@ERROR <> 0
BEGIN
ROLLBACK

RAISERROR ('Error when update TableB.', 16, 1)
RETURN
END

COMMIT


Edited by - swissivan on 02/26/2013 22:10:06

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 02/26/2013 :  22:42:27  Show Profile  Reply with Quote
why not use TRY CATCH?

http://msdn.microsoft.com/en-in/library/ms179296(v=sql.105).aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

swissivan
Starting Member

30 Posts

Posted - 02/26/2013 :  23:00:44  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

why not use TRY CATCH?

http://msdn.microsoft.com/en-in/library/ms179296(v=sql.105).aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





I am new to store procedure, could you please explain the pros and cons of .NET Try Catch VS Store Procedure Rollback? Many thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 02/26/2013 :  23:04:09  Show Profile  Reply with Quote
quote:
Originally posted by swissivan

quote:
Originally posted by visakh16

why not use TRY CATCH?

http://msdn.microsoft.com/en-in/library/ms179296(v=sql.105).aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





I am new to store procedure, could you please explain the pros and cons of .NET Try Catch VS Store Procedure Rollback? Many thanks.


its not .NET TRy Catch

Refer the article. It speaks of T-SQL TRY CATCH you can use in stored procedure itself.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

swissivan
Starting Member

30 Posts

Posted - 02/27/2013 :  02:21:13  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

quote:
Originally posted by swissivan

quote:
Originally posted by visakh16

why not use TRY CATCH?

http://msdn.microsoft.com/en-in/library/ms179296(v=sql.105).aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





I am new to store procedure, could you please explain the pros and cons of .NET Try Catch VS Store Procedure Rollback? Many thanks.


its not .NET TRy Catch

Refer the article. It speaks of T-SQL TRY CATCH you can use in stored procedure itself.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Sorry about the missundestanding, just read a paper about .net vs store procedure.

Try Catch works great. Thank you very much.

Edited by - swissivan on 02/27/2013 02:21:43
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 02/27/2013 :  04:10:33  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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.05 seconds. Powered By: Snitz Forums 2000