Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Using Transaction Rollback

Author  Topic 

swissivan
Starting Member

30 Posts

Posted - 2013-02-26 : 22:08:47
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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-26 : 22:42:27
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 - 2013-02-26 : 23:00:44
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

52326 Posts

Posted - 2013-02-26 : 23:04:09
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 - 2013-02-27 : 02:21:13
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.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-27 : 04:10:33
welcome

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

Go to Top of Page
   

- Advertisement -