begin transaction tran_bank;
declare @tran_error int;
set @tran_error = 0;
begin try
update bank set totalMoney = totalMoney - 10000 where userName = 'jack';
set @tran_error = @tran_error + @@error;
update bank set totalMoney = totalMoney + 10000 where userName = 'jason';
set @tran_error = @tran_error + @@error;
end try
begin catch
print '出現(xiàn)異常,錯(cuò)誤編號(hào):' + convert(varchar, error_number()) + '慨亲, 錯(cuò)誤消息:' + error_message();
set @tran_error = @tran_error + 1;
end catch
if (@tran_error > 0)
begin
--執(zhí)行出錯(cuò)爵卒,回滾事務(wù)
rollback tran;
print '轉(zhuǎn)賬失敗篱竭,取消交易';
end
else
begin
--沒有異常崎坊,提交事務(wù)
commit tran;
print '轉(zhuǎn)賬成功';
end
go