【APT】SqlServer游标使用
2024-10-17 22:07:10
use [ElephantCredit]
go
begin transaction tran_bank;
print '**脚本开始执行!';
declare @tran_error int ,
@negRuleId uniqueidentifier
set @tran_error = 0;
set @negRuleId = (select Id from [ElephantCredit].[dbo].[CreditRules] where [Provider]=N'CREDIT_PROVIDER_OPS' and [Code]=N'OPS_DOCTOR_RULE_N7_N')
begin
declare cursor_credit cursor scroll for
(select CreditId from [ElephantCredit].[dbo].[CreditHistories] where RuleId = @negRuleId
group by CreditId
) open cursor_credit;
declare @CreditId uniqueidentifier,
@FinalCredit int=0,
@OldCredit int=0,
@PeakCredit int=0
fetch first from cursor_credit into @CreditId;
while (@@fetch_status = 0)
begin --0.calculate the final credits/ignore these negative rule records.
set @FinalCredit = (select case when (sum(cast([value] as int)))<0 then 0 else sum(cast([value] as int)) end
from [ElephantCredit].[dbo].[CreditHistories] where CreditId=@CreditId and RuleId != @negRuleId) if (@FinalCredit is null)
BEGIN
--break;
goto _out
end --1.update credits principal using the latest credit
set @OldCredit = (select AvailableValue from [ElephantCredit].[dbo].[Credits] where Id=@CreditId)
set @PeakCredit = (select PeakValue from [ElephantCredit].[dbo].[Credits] where Id=@CreditId) update [ElephantCredit].[dbo].[Credits] set
AvailableValue = @FinalCredit,
UpdatedTime =GETDATE()
where Id=@CreditId
print '---creditId:'+ cast(@CreditId as nvarchar(50)) + ',重置成新积分值:'+ cast(@FinalCredit as varchar) +',旧值:' + cast(@OldCredit as varchar) + ',峰值:' + cast(@PeakCredit as varchar); --2.update groups principal
update [ElephantCredit].[dbo].[Growths] set
[Value] = @FinalCredit,
UpdatedTime =GETDATE()
where ExternalId=(select RefId from [ElephantCredit].[dbo].[Credits] where Id=@CreditId)
print '---成长值同步更新完毕!' ; _out:
fetch next from cursor_credit into @CreditId; end
close cursor_credit;
deallocate cursor_credit; --3.clear all the dirty data
delete from [ElephantCredit].[dbo].[CreditHistories] where RuleId = @negRuleId
print '清理全部脏数据完毕';
end
if (@tran_error > 0)
begin
rollback tran;
print '**脚本执行失败!已回滚';
end
else
begin
commit tran;
print '**脚本执行成功!';
end
go --Helper:
--select * from CreditHistories
--select * from Credits --where Id='9416FEBA-C19F-4718-80D5-F01643D57DE5'
--select * from CreditRules
最新文章
- hellocharts折线图与柱状图的上下结合酷炫效果(学习笔记)
- 谢欣伦 - OpenDev原创教程 - 网络设备查找类CxNetworkHostFind &; CxNetworkAdapterFind
- Deep learning:四十八(Contractive AutoEncoder简单理解)
- 利用ZTree链接数据库实现 [权限管理]
- LinkedList其实就那么一回事儿之源码分析
- TFS服务器(微软源代码管理服务器)上彻底删除项目
- SR4K的API使用(libMesaSR.dll)
- mybatis重拾---部署官方demo
- iOS8 UISearchViewController搜索功能讲解
- [TJOI 2016&;HEOI 2016]排序
- python ddt数据驱动(简化重复代码)
- pe文件头详解
- Sublime Text 3(3207)安装
- 《Oracle DBA工作笔记:运维、数据迁移与性能调优》 PDF 下载
- 【python】多进程与mongo
- webpack4 系列教程(六): 处理SCSS
- [Oracle]快速构造大量数据的方法
- java之静态方法与非静态方法
- DAX/PowerBI系列 - 参数表(Parameter Table) - 大客户分析(Top N)
- python实现Excel删除特定行、拷贝指定行操作