举个事例说明,以张三向李四在银行转账为例:- BEGIN TRANSACTION
- DECLARE @errorSum INT
- SET @errorSum=0
- UPDATE bank SET saveMoney=saveMoney-1000 WHERE customerName='张三'
- SET @errorSum=@errorSum+@@ERROR
- UPDATE bank SET saveMoney=saveMoney+1000 WHERE customerName='李四'
- SET @errorSum=@errorSum+@@ERROR
- IF @errorSum<>0
- BEGIN
- PRINT '交易失败,回滚事务'
- ROLLBACK TRANSACTION
- END
- ELSE
- BEGIN
- PRINT '交易成功,提交事务'
- COMMIT TRANSACTION
- END
- GO
复制代码 |