Full access to SQLServer exceptions and isolated transactions!

Source: Internet
Author: User
I. First, let's start with "Error" in SQL Server. The Error levels in SQL are 16, but the results are different. Select * from an external table if @ error0print this does not output goraiserror (,) if @ error0print this output goexec (select * from an external table) if @@

I. First, let's start with "Error" in SQL Server. The Error levels in SQL are 16, but the results are different. Select * from an external table if @ error0 print ', there is no output 'go raiserror) if @ error0 print 'output 'go exec ('select * from an external table') if @@

I. First, let's start with "Error" in SQL Server. The Error levels in SQL are 16, but the results are different.

Select * from an external table
If @ error <> 0
Print 'no output'
Go

Raiserror ('', 16, 3)
If @ error <> 0
Print 'output this output'
Go

Exec ('select * from an external table ')
If @ error <> 0
Print 'output this output'
Go

Exec sp_executesql N 'select * from a non-table'
If @ error <> 0
Print 'output this output'

In this way, you can find that execute suspicious SQL through exec or sp_executesql, so that you can catch the error that is aborted with an exception later.


Ii. Lead to isolated transactions:
1. isolated transactions

Select @ trancount the number of active transactions for the current connection -- the number of active transactions for the current connection is 0

Begin tran

Select * from an external table
If @ error <> 0
Begin
Print 'is not executed here! '
If @ trancount <> 0 rollback tran
End

Commit tran

Select @ trancount the number of active transactions of the current connection -- after execution, you can see that the number of active transactions of the current connection is 1, and the repeated execution will accumulate each time, which is very resource-consuming.

It should be that rollback is not rolled back at all.

2. Use existing methods to solve isolated transactions


Print @ trancount print 'number of active transactions for the current connection' -- the number of active transactions for the current connection is 0

If @ trancount <> 0 rollback tran -- write here to keep the isolated transaction only until your next process is called
Begin tran

Select * from an external table
If @ error <> 0
Begin
Print 'is not executed here! '
If @ trancount <> 0 rollback tran
End

Commit tran

--- After execution, you can see that the number of active transactions currently connected is 1, but repeated execution will not accumulate.
Print @ trancount print 'number of active transactions for the current connection'

3. Use set xact_abort to control the execution process of some errors that violate the constraints.

Create table Table1 (a int check (a> 100 ))
Go

Set xact_abort on
Begin tran
Insert table1 values (10)
Print 'not executed here'
Commit tran
Go

Print ''print '============================================ ============== 'print''

Set xact_abort off
Begin tran
Insert table1 values (10)
Print 'executed here'
Commit tran

Go
Drop table table1

However, set xact_abort does not work for compilation errors, and it also produces isolated transactions.

Set xact_abort on
Begin tran
Insert a table that is not in values (10)
Print 'not executed here'
Commit tran
Go

Print ''print '============================================ ============== 'print''

Set xact_abort off
Begin tran
Insert a table that is not in values (10)
Print 'not executed here'
Commit tran
Go

Select @ trancount the number of active transactions currently connected --- two isolated transactions
If @ trancount <> 0 rollback tran


Note the various SQL errors and isolated transactions in t-SQL programming. Be careful with the trap of isolated transactions and avoid wasting or isolating resources as much as possible, microsoft publicly announced that the next version of SQLServe Yukon will have a built-in Exception Handling syntax. At that time, you can use the code to better control unexpected errors.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.