首页 > 代码库 > sqlserver 2008实现的转账事务模拟
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
sqlserver 2008实现的转账事务模拟