Reference https://www.cnblogs.com/JentleWang/p/3654603.html
https://blog.csdn.net/tuzhen007/article/details/11183961
create proc proc_example as begin --declare a variable to store the number of transactions currently opened declare @exist_trancount int select @exist_trancount = @@trancount if @exist_trancount > 0 --Create transaction savepoint save transaction tran_proc else -- start a new transaction begin transaction tran_proc /* Stored procedure business processing code ········· */ if @@error<>0 goto error if @exist_trancount = 0 -- commit transaction commit tran tran_proc return 1 error: --Rollback transaction or transaction savepoint rollback transaction tran_proc return -1 end
- A committed transaction cannot be undone or rolled back.
- @@trancount is equal to 0 when there are no open transactions.
- Execute the begin tran [tranName] statement to increment @@trancount by 1.
- Execute the commit tran [tranName] statement to decrease @@trancount by 1.
- Executing a rollback tran rolls back the entire transaction and sets @@trancount to 0.