ch10-12.zip
资源内容介绍
ch10-12.zip use librarygo--创建帐户表bank--if exists(select * from sys.objects where name='bank') drop table bankcreate table bank(customerName char(10), --顾客姓名currentMoney money --当前余额)goalter table bank add constraint CK_currentMoney check(currentMoney>=1)insert into bank(customerName,currentMoney)select '张三',1000 unionselect '李四',1select * from bankgouse librarygo--恢复原来的数据--update bank set currentMoney=currentMoney-1000 where customerName='李'set nocount on --不显示受影响的行数print '查看转帐事务前的余额'select * from bankgobegin transactiondeclare @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 bankif @errorSum>0 begin print '交易失败,回滚事务.' rollback transaction endelse begin print '交易成功,提交事务,写入硬盘,永久保存!' commit transaction endgoprint '查看转帐后的余额'select * from bankgo