Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Rollback Script 1

Status
Not open for further replies.

kav123

Programmer
Jan 12, 2005
210
GB
Hi, I will be running an update script on a table on the live database servers. However, this time i thought of writing a script to do the updates, instead of running individual queries. I also want a rollback script. Any ideas how to write a rollback script
 
Just do all updates in tranasction and if some of them failed then rollback the transaction:
Code:
DECLARE @Error int
SET @Error = 0
BEGIN TRANSACTION
  UPDATE ....
  SET @Error = @Error + @@ERROR
  UPDATE ....
  SET @Error = @Error + @@ERROR
  UPDATE ....
  SET @Error = @Error + @@ERROR
  .....
IF @Error <> 0
   ROLLBACK TRANSACTION
ELSE
   COMMIT TRANSACTION
you must store Error code after each statement, because every new line from the script clear the previous error.


Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Thanks so much for that, that was helpful
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top