As you all might know, one of the downsides of the @@ERROR
variable approach to catch the error is that we must check the value of this variable after each and every DML /DDL statement to determine if an error occurred and, if so, to rollback the transaction. With SQL Server 2005's TRY...CATCH block, however, these types of scripts are greatly simplified.
BEGIN TRANBEGIN TRYSELECT 1 / 0 infiniteCOMMIT TRANEND TRYBEGIN CATCHRAISERROR ( 'err', 16, 1 )SELECT @@trancount beforeROLLBACK TRANSELECT @@trancount afterEND CATCHGO
Does the order in which we put BEGIN TRAN..BEGIN TRY matters , let us check
BEGIN TRYBEGIN TRANSELECT 1 / 0 infiniteCOMMIT TRANEND TRYBEGIN CATCHRAISERROR ( 'err', 16, 1 )SELECT @@trancount beforeROLLBACK TRANSELECT @@trancount afterEND CATCHGO
Technorati Tags: begin try begin tran,begin tran begin try,sql server 2005,sql server 2008,error handling