1. 程式人生 > >SQL添加事務處理

SQL添加事務處理

doc IT ransac In report 完成 CI 添加 tab

--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 @errorSum=@errorSum + @@error
update tRecipe_Detail
set PerformUserID = @OperUserID, DocAdvStatus = 5,
PerformDate = GETDATE()
where ADVID in (select ADVID from #CheckreqAccState)
set @errorSum=@errorSum+@@error
update tRecipe_Detail_FeeItem
set PerformUserID = @OperUserID, DocAdvStatus = 5,
PerformDate = GETDATE()
where ADVID in (select ADVID from #CheckreqAccState)
set @errorSum=@errorSum+@@error
update tChargeDetail
set PerformUserID = @OperUserID, DocAdvStatus = 5,
PerformDate = GETDATE()
where ADVID in (select ADVID from #CheckreqAccState)
set @errorSum=@errorSum+@@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添加事務處理