Igor
Igor

Reputation: 4003

Transaction RollBack when success

How to create a query that tests an "update command" and the same time rollback data? Would be to have a feedback like: ALL RIGHT! Everything worked! Note: Using SQL Transaction

Upvotes: 1

Views: 802

Answers (2)

sean
sean

Reputation: 1205

Use transactions:

 BEGIN TRANSACTION

 Update table1 set col1 = col1 + col2 where col3 = 'X'

 Select * from table1 where Col3 = 'X'

 ROLLBACK TRANSACTION

Upvotes: 1

BrettKB
BrettKB

Reputation: 205

This should return a 1 if there's an error and ROLLBACK no matter the result:

DECLARE @ErrorCheck int
SET @ErrorCheck = 0

BEGIN TRANSACTION

    --RUN UPDATE STATEMENT HERE
    IF (@@ERROR != 0)
        SET @ErrorCheck = 1

ROLLBACK TRANSACTION

SELECT @ErrorCheck

Upvotes: 2

Related Questions