首页 新闻 会员 周边 捐助

存储过程怎么写事务回滚

0
悬赏园豆:20 [待解决问题]

create procedure T_UserDealDetails_Insert@UserID int,@DealTime datetime,@DetailMoney money,@FormalitiesFees money,@SchemeNumber nvarchar(50),@RelatedUserID int,@PayNumber nvarchar(50),@PayBank nvarchar(50),@Memo nvarchar(50),@OperatorUserID int,@AlipayID nvarchar(50),@AlipayName nvarchar(50),@TenpayID nvarchar(50),@TenpayName nvarchar(50) 
AS
 insert into [T_UserDealDetails]([UserID],[DealTime],[DealType],[DetailMoney],[FormalitiesFees],[SchemeNumber],[RelatedUserID],[PayNumber],[PayBank],[Memo],[OperatorUserID],[AlipayID],[AlipayName],[TenpayID],[TenpayName])VALUES(@UserID,@DealTime,1,@DetailMoney,@FormalitiesFees,@SchemeNumber,@RelatedUserID,@PayNumber,@PayBank,@Memo,@OperatorUserID,@AlipayID,@AlipayName,@TenpayID,@TenpayName)update T_Users set  Balance=@DetailMoney  where T_Users.UserID=@UserID

这个写事务回滚怎么写啊/? 
我还没用过事务回滚。听老师提过一次。然后也没写过代码。。现在要写了。呃。不会呃。请教下大家。教一把吧。谢了喔。 

zsstone的主页 zsstone | 初学一级 | 园豆:100
提问于:2011-07-01 14:55
< >
分享
所有回答(2)
0

在存储过程后面加上:
if @@error<>0
      rollback tran
 else
     commit tran

avic_ren | 园豆:373 (菜鸟二级) | 2011-07-01 15:39
哈、思路 是这样的。谢了咯
支持(0) 反对(0) zsstone | 园豆:100 (初学一级) | 2011-07-01 17:54
0

--打开自动回滚,出错时会自动回滚当前事务

set xact_abort on

--开始事务

begin tran

 insert into [T_UserDealDetails]([UserID],[DealTime],[DealType],[DetailMoney],[FormalitiesFees],[SchemeNumber],[RelatedUserID],[PayNumber],[PayBank],[Memo],[OperatorUserID],[AlipayID],[AlipayName],[TenpayID],[TenpayName])VALUES(@UserID,@DealTime,1,@DetailMoney,@FormalitiesFees,@SchemeNumber,@RelatedUserID,@PayNumber,@PayBank,@Memo,@OperatorUserID,@AlipayID,@AlipayName,@TenpayID,@TenpayName)update T_Users set  Balance=@DetailMoney  where T_Users.UserID=@UserID

--提交事务

commit tran

镆铘 | 园豆:361 (菜鸟二级) | 2011-07-01 17:45
嗯 。谢谢,是这样写的
支持(0) 反对(0) zsstone | 园豆:100 (初学一级) | 2011-07-01 17:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册