写两条SQL语句,一担一条有问题,就回滚!使用事务的一致性 该怎么写哦?

解决方案 »

  1.   

    set xact_abort on
    begin tran
      update a ...
      update b ...
    commit tran
      

  2.   

    begin tran
    begin try
        update tb1...
        update tb2...
        IF @@trancount>0 
           commit tran
    end try
    begin catch
        IF @@trancount>0 
            rollback tran
    end catch
      

  3.   

    set xact_abort on
    begin tran
      update  ...
      update ...
    commit tran--当 SET XACT_ABORT 为 ON 时,如果执行 Transact-SQL 语句产生运行时错误,则整个事务将终止并回滚。 
    --当 SET XACT_ABORT 为 OFF 时,有时只回滚产生错误的 Transact-SQL 语句,而事务将继续进行处理。
    --如果错误很严重,那么即使 SET XACT_ABORT 为 OFF,也可能回滚整个事务。