Batuhan Ozdal Batuhan Ozdal - 7 months ago 26
SQL Question

Rollback if null

I was reading articles about rollback transaction and can't find something like I need. Assume I have a user table. User can update his/her records but I want to rollback if entered value is null. I know there are lots of simple ways to do this like checking with c# if it is null and things like that but point in here is understanding rollback. How can I use rollback with if(null) code ?

Answer

Rollback is used with a transaction, a rollback restores the state to what it was when begin tran was executed.

BEGIN TRAN 'tran1'
       INSERT INTO Whatever table...
      IF(... check for null)
      BEGIN 
        ROLLBACK TRAN 'tran1'
        RETURN 0
      END
COMMIT TRAN 'tran1'