要同时删除几个表
如果一个表删除不成功将回滚事务
这个事务该如何写呢,试了半天了,还是不回滚;

解决方案 »

  1.   

    BEGIN TRANSACTION TranName
       update....
       delete ....
    IF (@@ERROR <>0) 
       ROLLBACK TRANSACTION TranName
    ELSE
       COMMIT TRAN TranName
      

  2.   


    begin trandelete from table_Pqs where ......
    if @@error<>0    goto errdelete from table_Pqh where ......
    if @@error<>0    goto errdelete from table_Pyz where ......
    if @@error<>0    goto errif @@trancount>0     commit tran
    returnerr:
       if @@trancount>0     rollback tran
      

  3.   

    BEGIN TRAN
    BEGIN TRY
    DELETE FROM table1 where……
    DETELE FROM table2 where……
    COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
    IF XACT_STATE()<>0
    ROLLBACK TRANSACTION
    END CATCH如果你是SQL 2005 推荐使用这种方式
      

  4.   

    设置set xact_abort on即可。如:
    set xact_abort onbegin tran...
    commi tran