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