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 '出现异常,错误编号:' + convert(varchar, error_number()) + ', 错误消息:' + error_message();
set @tran_error = @tran_error + 1;
end catch
if (@tran_error > 0)
begin
--执行出错,回滚事务
rollback tran;
print '转账失败,取消交易';
end
else
begin
--没有异常,提交事务
commit tran;
print '转账成功';
end
go