sql set xact_abort on 用例
2016-03-15 11:32
351 查看
set xact_abort on
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
设置事务回滚的 当为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
相关文章推荐
- mongodb启动命令
- mysql数据库去重复
- debian8下给postgresql9.5编译配置pgpool-II-3.5
- Linux下安装Redis
- Oracle导出excel数据
- Python简单连接MongoDB数据库的方法
- mongodb - Master Slave Replication
- 对不可用for update 的数据库管理工具如何复制记录
- oracle 导入DMP文件时IMP-00013: 只有 DBA 才能导入由其他 DBA 导出的文件
- VS2010+Oracle11+Entity Framework4.1环境搭建及常见问题(转)
- 阿里云使用之ssh远程Ubuntu服务器的数据库乱码问题
- sql时间格式化
- 数据库编码问题,搜索中文内容获取不到内容
- 将 Entity Framework、LINQ 和 Model-First 用于 Oracle 数据库
- Mysql 行列转置
- oracle全托管驱动Oracle.ManagedDataAccess
- MySQL的列类型范围
- Redis Sentinel 机制与用法(二)
- sqlite存储图片
- MySql主主(主从)同步配置详解