SQL 存储过程编写事务
/**//*--开始事务--*/ begin transaction declare @errorSum int --定义变量,用于累计事务执行过程中的错误 /**//*--转帐--*/ update bank set currentMoney=currentMoney-800 where customerName='张三' set @errorSum=@errorSum+@@error --累计是否有错误 update bank set currentMoney=currentMoney+800 where customerName='李四' set @errorSum=@errorSum+@@error --累计是否有错误 print '查看转帐事务过程中的余额' select * from bank /**//*--根据是否有错误,确定事务是提交还是回滚--*/ if @errorSum>0 begin print '交易失败,回滚事务.' rollback transaction end else begin print '交易成功,提交事务,写入硬盘,永久保存!' commit transaction end go print '查看转帐后的余额' select * from bank go