When the value of XACT_ABORT is set to ON, the error – “The current transaction cannot be committed” occurs when another transaction is begun before a commit or rollback of the previous executing transaction. Here are few instances that invite this error:

Syntax


  1. SET XACT_ABORT { ON | OFF }
Explanation

  • When ON, if there is a run-time error, the entire transaction is terminated and error is thrown.
  • When OFF, the Transact-SQL statement that raised the error is rolled back without any error indication.

Example:

Inserting duplicate value in a Primary Key column within a transaction.

  1. drop table EmpSalary
  2. drop table Emp
  3. create table Emp([EmpId][int] primary key)
  4. create table EmpSalary(Salary money)
  5. set xact_abort on
  6. begin
  7. try
  8. begin tran
  9. insert into Emp([EmpId]) values(1)
  10. insert into Emp([EmpId]) values(2)
  11. insert into Emp([EmpId]) values(1) --duplicate value
  12. commit
  13. end
  14. try
  15. begin
  16. catch
  17. insert into EmpSalary(Salary) values(10000)
  18. if @ @trancount > 0
  19. rollback
  20. end
  21. catch

As seen in the above picture, the error occurs when the XACT_ABORT is set to ON.

You can find below that the same query executes with error and all insert statements are rolled back when the value is set to OFF.