转账事务

先建立数据环境(帐户表)

<shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><strong><img alt="" src="https://p-blog.csdn.net/images/p_blog_csdn_net/cnlht/1.JPG"><br></strong></shapetype>1.转账事务(使用在存储过程中)

if exists(select name from sysobjects
where name = 'zhuanzhang' and type='p')
drop procedure zhuanzhang
go

--建立一个带有参数的转账存储过程
-- @zhOUT转出帐户帐号
-- @zhIN 转入帐户帐号
--@JinE 金额
create procedure zhuanzhang
@zhOUT varchar(8),@zhIN varchar(8),@JinE int
as

--帐户A向帐户B转账事务

begin transaction

--记录点
save transaction befor_zhuanzhang

--A帐户转出一定金额
declare @xianYou int
select @xianyou=金额 from 帐户表
where 帐户编号=@zhOUT

if @xianyou<@JinE
begin
print '金额不足!'
return
end

update 帐户表 set 金额=金额-@JinE
where 帐户编号=@zhOUT

--B帐户转入一定金额
update 帐户表 set 金额=金额+@JinE
where 帐户编号=@zhIN

if@@Error<>0 or @@rowcount=0
begin
rollback transaction befor_zhuanzhang
print '转账出错错误。'
return
end

commit transaction

2.事务测试(执行存储过程zhuanzhang)

1、execute zhuanzhang 'A','B',5000
2、execute zhuanzhang 'C','D',5000
注意结果变化。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值