Begin
Set NOCOUNT ON; --不返回影響行數
Set XACT_ABORT ON; --使用存儲過程執行事務需要開啟XACT_ABORT參數(默認為OFF)
delete from table1 where name='' --刪除數據sql1
begin tran tran1 --開始一個事務tran1
delete from table1 where name='' --刪除數據sql2
save tran tran2 --保存一個事務點tran2
update table2 set name='' where id='' --修改數據sql3
if @@error>0 --判斷修改數據有沒有錯誤(@@error表示返回與@@ERROR 最近的語句(即sql3)的非零的錯誤碼,沒有錯誤則返回0)
begin
rollback tran tran2 --回滾事務到tran2的還原點
commit tran tran1 --提交事務tran1
end
else --沒有出錯則提交事務tran1
commit tran tran1 --提交事務tran1
End