加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sqlserver 2008实现的转账事务模拟

发布时间:2020-12-12 12:53:55 所属栏目:MsSql教程 来源:网络整理
导读:use master drop table customer create table customer( id int primary key, account decimal check(account0), name varchar(20) ) --use master alter table dbo.customer add check(customer.account0) select * from customer insert into customer va

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,'王五');

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读