Errors During Transaction Processing If an error prevents the successful completion of a transaction, SQL Server automatically rolls back the transaction and frees all resources held by the transaction. If the client's network connection to an instance of the Database Engine is broken, any outstanding transactions for the connection are rolled back when the network notifies the instance of the break. If the client application fails or if the client computer goes down or is restarted, this also breaks the connection, and the instance of the Database Engine rolls back any outstanding connections when the network notifies it of the break. If the client logs off the application, any outstanding transactions are rolled back.If a run-time statement error (such as a constraint violation) occurs in a batch, the default behavior in the Database Engine is to roll back only the statement that generated the error. You can change this behavior using the SET XACT_ABORT statement. After SET XACT_ABORT ON is executed, any run-time statement error causes an automatic rollback of the current transaction. Compile errors, such as syntax errors, are not affected by SET XACT_ABORT. For more information, see SET XACT_ABORT (Transact-SQL).When errors occur, corrective action (COMMIT or ROLLBACK) should be included in application code. One effective tool for handling errors, including those in transactions, is the Transact-SQL TRY…CATCH construct. For more information with examples that include transactions, see Using TRY...CATCH in Transact-SQL and TRY...CATCH (Transact-SQL).===============================
第一句:If an error prevents the successful completion of a transaction, SQL Server automatically rolls back the transaction and frees all resources held by the transaction. 
第二句:When errors occur, corrective action (COMMIT or ROLLBACK) should be included in application code. 到底什么时候由sql server roll back,什么时候程序员自己写代码roll back?

解决方案 »

  1.   

    当你写一句批量执行的sql,出错后会自动回滚当你写n句sql语句,出错了只有当前这句会回滚,已经执行成功的,如果程序员不自己开启事物回滚那就提交了。
      

  2.   

    在处理事务期间发生错误,你把存储过程帖出来差不多;
    每个begin tran 要对应有commit tran和rollback tran
    你看是不是在语法上缺少了完整语句?