博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【APT】SqlServer游标使用
阅读量:5261 次
发布时间:2019-06-14

本文共 2934 字,大约阅读时间需要 9 分钟。

use [ElephantCredit]gobegin 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 '**脚本执行失败!已回滚';    endelse    begin         commit tran;        print '**脚本执行成功!';    endgo --Helper: --select * from  CreditHistories --select * from Credits --where Id='9416FEBA-C19F-4718-80D5-F01643D57DE5' --select * from CreditRules

 

转载于:https://www.cnblogs.com/x-poior/p/8492162.html

你可能感兴趣的文章
React Native iOS 离线包
查看>>
Command Analyze failed with a nonzero exit code
查看>>
________________”一切都变了”被遗忘的 label
查看>>
记录点滴18
查看>>
loadrunner使用随机值
查看>>
C# .NET小数位数不足自动补充
查看>>
css属性操作
查看>>
MySQL数据库远程连接的配置方案
查看>>
Spring注解设置需要扫描的包
查看>>
Discuz建站教程:本地安装discuz网站
查看>>
【转载】.NET/C#-uploadify视频文件or大文件上传
查看>>
daemon框架
查看>>
偶入博客园
查看>>
栈和队列
查看>>
人脸识别的数据库汇总
查看>>
64位版本为什么叫amd64,而不是intel64
查看>>
MySQL 可以用localhost 连接,但不能用IP连接的问题,局域网192.168.*.* 无法连接mysql...
查看>>
JAVA8 ARRAY、LIST操作 汇【5】)- JAVA8 LAMBDA LIST统计(求和、最大、最小、平均)...
查看>>
bzoj1135:[POI2009]Lyz
查看>>
Linux0.11内核--系统调用机制分析
查看>>