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