请问zzutlink(来溜达):
只要在最后加
  if @@error>0 
    rollback tran aaa1
就能使所有的错误触发回滚吗?

解决方案 »

  1.   

    不能,每句都加一个if @@error>0 .........
      

  2.   

    begin tran aaa1
    declare
      @lvError int
    set @lvError=0  insert...
      set @lvError=@lvError+@@ERROR
      update...
      set @lvError=@lvError+@@ERROR
      delete...
      set @lvError=@lvError+@@ERROR
      if @@lvError>0 
        rollback tran aaa1
      else
      commit tran aaa1
      

  3.   

    那么用set XACT_ABORT on 而不用判断@@error
    会有问题吗?
    我一直在这样用,还没有发现问题,
    请问哪位高手使用过?
      

  4.   

    原因很简单因为sql server在每条insert或update,delete后就自动触发了commit tran
    所以你用rollback只能是返回上一条正确insert,update,delete前的tran
      

  5.   

    那么用set XACT_ABORT on 而不用判断@@error
    会有问题吗?
    我一直在这样用,还没有发现问题,
    请问哪位高手使用过? 
      

  6.   

    set XACT_ABORT on很好,我们公司(珠海远方财务)一直这样用,
    我们是正版,有微软的技术支持。
      

  7.   

    XACT_ABORTSpecifies termination handling for the current procedure. 
    Setting XACT_ABORT ON terminates the transaction if any error is encountered. If one statement raises an error, the entire transaction is terminated and rolled back.
    Setting XACT_ABORT option is set to OFF terminates only the SQL statement that raised the error. The transaction will continue.