sqlserver 2008实现的转账事务模拟

use master

drop table customer
create table customer(
id int primary key,
account decimal check(account>0),
name varchar(20)
)

--use master
alter table dbo.customer
add check(customer.account>0)

select * from customer
insert into customer values(2012090101,2000,‘张三‘);
insert into customer values(2012090102,1000,‘李四‘);
insert into customer values(2012090103,1000,‘王五‘);

begin transaction
declare @sum int
set @sum=0
update customer set account-=200 where customer.id=2012090101
set @sum+=@@ERROR
update customer set account+=200 where customer.id=2012090102
set @sum+=@@ERROR

--update customer set account-=100 where customer.id=2012090103
if @sum<>0
begin
 print ‘转账失败‘
 rollback transaction
end
else
 begin
 print ‘成功转账‘
 commit transaction
 end
go
select * from customer


 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。