SQL添加事务处理
Posted 小美lmt
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL添加事务处理相关的知识,希望对你有一定的参考价值。
--modi by lmt
declare
@errorSum int --记录错误数
begin
Create table #CheckreqAccState(CheckReqID varchar(20), ADVID varchar(20), DocAdvStatus int, AccState varchar(20))
set @SqlTxt = ‘insert into #CheckreqAccState(CheckReqID, ADVID, DocAdvStatus, AccState)
select A.CheckReqID, A.ADVID, B.DocAdvStatus, AccState from GHMTDB..tCheckReq_Main(nolock) A Left join GHMTDB.dbo.tRecipe_Detail(nolock) B on A.ADVID = B.ADVID
where A.CheckReqID in (‘ + @CheckReqID + ‘)‘
exec(@SqlTxt)
if exists(select * from #CheckreqAccState where DocAdvStatus < 3)
begin
set @ReqInfor = ‘确费失败:申请单号中【‘ + @CheckReqID +‘】存在未扣费的记录,请核实‘
drop table #CheckreqAccState
goto Err
end
else
begin transaction
Update GHMTDB..tCheckReq_Main
Set State = 3, ReportUser = @OperUserID, ReportDate = GetDate()
Where CheckReqID in (select CheckReqID from #CheckreqAccState)
set @[email protected] + @@error
update tRecipe_Detail
set PerformUserID = @OperUserID, DocAdvStatus = 5,
PerformDate = GETDATE()
where ADVID in (select ADVID from #CheckreqAccState)
set @errorSum[email protected][email protected]@error
update tRecipe_Detail_FeeItem
set PerformUserID = @OperUserID, DocAdvStatus = 5,
PerformDate = GETDATE()
where ADVID in (select ADVID from #CheckreqAccState)
set @[email protected][email protected]@error
update tChargeDetail
set PerformUserID = @OperUserID, DocAdvStatus = 5,
PerformDate = GETDATE()
where ADVID in (select ADVID from #CheckreqAccState)
set @[email protected][email protected]@error
drop table #CheckreqAccState
if @errorSum > 0
begin
set @ReqInfor = ‘执行失败‘
select @ReqInfor ‘ReqInfor‘,@ChargeSN ‘ChargeSN‘
rollback transaction
end
else
begin
set @ReqInfor = ‘执行完成‘
select @ReqInfor ‘ReqInfor‘,@ChargeSN ‘ChargeSN‘
commit transaction
end
end
end --end else if @DataSource = ‘门诊‘
end--modi end
以上是关于SQL添加事务处理的主要内容,如果未能解决你的问题,请参考以下文章