A lot of people do not know is that SQL supports the GOTO command and labels just like good old basic. An example is worth a 3.3337 words:

 

    BEGIN TRAN UpdateDetails
        
        update     details
        set        payer = @payer
        where      payerId=@PayerId
        
        IF @@error <> 0
            GOTO   Errorhandler

        update     payer
        set        lastActivityDate=getdate()
        where      payerId = @PayerId

        IF @@error <> 0
            GOTO Errorhandler

    commit tran UpdateDetails
ErrorHandler:
        BEGIN
    ROLLBACK TRAN UpdateDetails
        END

As you can see that after every statement we run we check the error status and if there is an error we go to ErrorHandler. Pretty easy (and ugly)

Now you might wonder why I am checking the @@error after every statement and not just at the end. SQL's @@error gets set to 0 on a successfull statement run. So this SQL snippet

select * from tabledoesnotexist

select * from tableDOESexist

select @@error

Will return 0 because we have run a select against the table that does exist before we check the status of @@error which resets it back to 0. This is why it is incredibly important to test after every method call. I was reading last night that sql2005 supports Try / Catch semantics that makes things a lot easier and cleaner to work with