2013-07-08

TSQL - Error catching basic structure (try/catch/rollback)

begin try
begin tran


       -- YOUR STATEMENTS HERE


       commit tran
end try
begin catch

       -- Display error
       /*
       SELECT
              ERROR_NUMBER() AS ErrorNumber,
        ERROR_MESSAGE() AS ErrorMessage
       ;
       */
       print('ERROR...');
       print( 'Number: ' +  cast(ERROR_NUMBER() as varchar(50)) );
       print( 'Message: ' + cast(ERROR_MESSAGE() as varchar(max)));


       -- Announce rollback
       print('Rolling back!');
       rollback tran


end catch


No comments:

Post a Comment