码迷,mamicode.com
首页 > 其他好文 > 详细

用新学的知识 写了一段小代码

时间:2017-03-12 16:28:10      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:customer   mit   errors   bank   ble   tab   comm   erro   object   

use myschool
go
if exists(select*from sysobjects where name=‘bank‘)
drop table bank
go
create table bank
(
customername char(10),
currentmoney money
)
go
alter table bank
add constraint ck_currentmoney check(currentmoney>=0)
go
insert into bank(customername,currentmoney) values(‘张三‘,1000)
insert into bank(customername,currentmoney) values(‘李四‘,12)
insert into bank(customername,currentmoney) values(‘王二麻子‘,3)
go

--select*from bank


-- update bank set currentmoney=currentmoney-10000
-- where customername=‘王二麻子‘
-- update bank set currentmoney=currentmoney+10000
--where customername=‘李四‘
-- go






use myschool
go
print‘查看转账之前余额‘
select *from bank
go
begin transaction
declare @errorsum int

set @errorsum=0
update bank set currentmoney=currentmoney-3
where customername=‘王二麻子‘
set @errorsum=@errorsum+@@ERROR
update bank set currentmoney=currentmoney+3
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

用新学的知识 写了一段小代码

标签:customer   mit   errors   bank   ble   tab   comm   erro   object   

原文地址:http://www.cnblogs.com/ctyy/p/6538091.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!