您的位置:首页 > 数据库

sql set xact_abort on 用例

2016-03-15 11:32 351 查看
set xact_abort on

设置事务回滚的
当为ON时,如果你存储中的某个地方出了问题,整个事务中的语句都会回滚
为OFF时,只回滚错误的地方

例子 :

ALTER  proc  [dbo].[BuChongZhaiQuan_SQl]

as
begin
set xact_abort on
begin transaction
--if exists(select 1 from InvestorApply where RedeemDate=(select ShouldPayDate=max(ShouldPayDate) from ActualRedemptionIncome) and CheckState=9)
if exists(select 1 from InvestorApply where  CheckState=9 and DeleteState=0)
--if(1<>1)
print '暂不补充比例'
else
begin
print '执行补充比例,第一步'

insert  into NewBuChong
select ApplicationID,ststus=0, REPLACE(CONVERT (VARCHAR(100),getdate(),23),'-',''),getdate()
--,ttt.真实匹配比例 ,TTT.LoanDate
from  tb_sanbiao_20160215_InvestorApplyID

end
if(@@error<>0)
begin
select 'Error'
rollback tran
return;
end
else
begin
commit;
select 'OK'
end
end

ALTER proc [dbo].[BuChongZhaiQuan_SQl]
as beginset xact_abort on begin transaction--if exists(select 1 from InvestorApply where RedeemDate=(select ShouldPayDate=max(ShouldPayDate) from ActualRedemptionIncome) and CheckState=9)if exists(select 1 from InvestorApply where CheckState=9 and DeleteState=0)--if(1<>1)print '暂不补充比例'else beginprint '执行补充比例,第一步'
insert into NewBuChong select ApplicationID,ststus=0, REPLACE(CONVERT (VARCHAR(100),getdate(),23),'-',''),getdate()--,ttt.真实匹配比例 ,TTT.LoanDatefrom (select tt.ApplicationID,tt.LoaneeName '借款人姓名', SUM(tt.LoanMatchRatio) as '真实匹配比例',TT.LoanDate from (select b.LoanMatchRatio,a.ApplicationID,a.LoaneeName,A.LoanDatefrom Loanee as aleft join LoanMatchDetails as b on b.LoaneeID=a.ID--left join LoanMatch as c on b.LoanMatchID=c.IDleft join InvestorApplyDetails as d on b.InvestorApplyDetailsID=d.IDleft join InvestorApply as e on d.InvestorApplyID=e.IDleft join LoaneeExpand as f on a.ID=f.LoaneeIDleft join Investor as i on e.InvestorID=i.IDwherea.LoanDate>'1900-01-01'and f.transferIsorNo='Y'----and a.AddTime<'2015-11-01'----and --and --a.ApplicationID in (

--)anda.LoaneeType=0 and a.DeleteState=0 and d.DeleteState=0 and e.DeleteState=0 and i.DeleteState=0 and e.CheckState in (3)and e.InvestorApplyType<>2and Status=0
)tt group by ApplicationID,LoaneeName ,LoanDate--,真实匹配比例 --having '真实匹配比例'<>1 ;
) ttt where ttt.真实匹配比例<1 -- abs(1-ttt.真实匹配比例)>0.00000001print '第2步'select ApplicationID,realRatio into #t from (select tt.ApplicationID,tt.LoaneeName '借款人姓名', SUM(tt.LoanMatchRatio) as realRatio from (select b.LoanMatchRatio,a.ApplicationID,a.LoaneeNamefrom Loanee as aleft join LoanMatchDetails as b on b.LoaneeID=a.IDleft join InvestorApplyDetails as d on b.InvestorApplyDetailsID=d.IDleft join InvestorApply as e on d.InvestorApplyID=e.IDleft join LoaneeExpand as f on a.ID=f.LoaneeIDleft join Investor as i on e.InvestorID=i.IDwherea.LoanDate>'1900-01-01'and f.transferIsorNo='Y'--and a.AddTime<'2015-11-01'
and a.ApplicationID in (select ApplicationID from NewBuChong where ststus=0 and PiCi= REPLACE(CONVERT (VARCHAR(100),getdate(),23),'-',''))anda.LoaneeType=0 and a.DeleteState=0 and d.DeleteState=0 and e.DeleteState=0 and i.DeleteState=0 and e.CheckState in (3)and e.InvestorApplyType<>2and Status=0
)tt group by ApplicationID,LoaneeName --,真实匹配比例 --having '真实匹配比例'<>1 ;
) ttt where abs(1-ttt.realRatio)>0.00000001
print '第3步' update Loanee set MatchRatio = t2.realRatio ,SurplusMatchRatio=1-t2.realRatio,UpdateTime=GETDATE()from Loanee t1,#t t2 where t1.ApplicationID = t2.ApplicationID and t1.DeleteState=0 and t1.LoaneeType=0
print '第4步';with cte as (select a.* from Loanee a right join #t b on a.ApplicationID=b.ApplicationID and LoaneeType=0 and DeleteState=0)insert into LoanMatchDetails select 2818693,cte.id,0,1, GETDATE(),cte.SurplusMatchRatio,-1,0 ,GETDATE(),29452866,1,newid() from cte
print '第5步';with cte as (select a.* from Loanee a right join #t b on a.ApplicationID=b.ApplicationID and LoaneeType=0 and DeleteState=0)update Loanee set SurplusMatchRatio=0 ,MatchRatio= 1 where ID in( select ID from cte )
print '第6步'UPDATE NewBuChong SET ststus=2 where ststus=0 and ApplicationID in (select ApplicationID from #t)end
if(@@error<>0)beginselect 'Error'rollback tranreturn;endelsebegincommit;select 'OK'endend
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: