Correct use of transactions in SQL Server

后端 未结 3 552
北恋
北恋 2020-11-28 17:20

I have 2 commands and need both of them executed correctly or none of them executed. So I think I need a transaction, but I don\'t know how to use it correctly.

What

3条回答
  •  天涯浪人
    2020-11-28 18:04

    At the beginning of stored procedure one should put SET XACT_ABORT ON to instruct Sql Server to automatically rollback transaction in case of error. If ommited or set to OFF one needs to test @@ERROR after each statement or use TRY ... CATCH rollback block.

提交回复
热议问题