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

【APT】SqlServer游标使用的更多相关文章

  1. Sqlserver游标复习

    经常写存储过程,但今天在游标使用过程中还是疏忽了一些事情,执行过程中一直执行不下去,后来直接sqlserver挂了,教训啊! 代码虽简单,望铭记: Create PROCEDURE [dbo].[te ...

  2. SqlServer游标的创建与使用

    前言 大家都对SqlServer视图.存储过程.触发器的创建与使用有一定的了解了,我们来看下什么是游标,怎么使用,什么时候用. SqlServer视图的创建与使用 SqlServer存储过程的创建与使 ...

  3. sqlserver 游标的使用

    declare @temp_temp uniqueidentifier--临时变量 DECLARE aaa CURSOR for select Id from A ------------------ ...

  4. SqlServer游标简介

    游标实例:             Declare MyCusror Cursor Scroll For Select * From Master_Goods Order By GoodsID Ope ...

  5. sqlserver 游标

    DECLARE ChangeInvCodeCursor CURSOR FOR SELECT A.name AS tablecolumn,C.name AS tablename FROM sys.col ...

  6. SQLServer游标(Cursor) (B)

    游标(Cursor)是处理数据的一种方法,为了查看或者处理结果集中的数据,游标提供了在结果集中一次以行或者多行前进或向后浏览数据的能力.我们可以把游标当作一个指针,它可以指定结果中的任何位置,然后允许 ...

  7. SQLServer 游标 (A)

    游标 游标分为客户端游标和服务器端游标.Sql通过游标可以对一个结果集进行逐行处理.对于使用服务器端游标的过程有:声明.打开.读取.关闭.释放. 1 声明游标 1.1 SQL-92标准的声明 Decl ...

  8. SQLserver游标原理和使用方法

    在数据库开发过程中,当你检索的数据只是一条记录时,你所编写的事务语句代码往往使用SELECT INSERT 语句.但是我们常常会遇到这样情况,即从某一结果集中逐一地读取一条记录.那么如何解决这种问题呢 ...

  9. (转)sqlserver游标概念与实例全面解说

    首先声明:该文章转自http://www.cnblogs.com/wudiwushen/archive/2010/03/30/1700925.html  的博客 引言  我们先不讲游标的什么概念,步骤 ...

随机推荐

  1. 3.AOP入门1.md

    目录 1.定义 1.1基本概念 2. 1.定义 1.1基本概念 AOP:aspect object programing面向切面编程 aop编程的要点在于关注点和切入点 关注点:指的是代码中的重复部分 ...

  2. 18.4 运行脚本 sudo ./launcher.sh 必须先给他权限 才能使用

    我们发现我们每次启动服务都要打一大堆命令,ng build --watch ,nodemon server.js ,等等 凡是重复的工作,我们要可以代码完成 要想运行它呢 你必须将你运行的上述(脚本中 ...

  3. 项目IDEA启动配置

    在所有java启动项中加入 -Djute.maxbuffer=2048000 tomcat 在catalina.bat 中第一行加入 set JAVA_OPTS=-Djute.maxbuffer=20 ...

  4. destoon手机端分页

    $pages = ''; $pagesize = 2; $offset = ($page-1)*$pagesize; $rr = $db->get_one("SELECT COUNT( ...

  5. Spring AOP @Aspect

    spring提供了两个核心功能,一个是IoC(控制反转),另外一个便是Aop(面向切面编程),IoC有助于应用对象之间的解耦,AOP则可以实现横切关注点(如日志.安全.缓存和事务管理)与他们所影响的对 ...

  6. 第六篇:Jmeter Ftp服务器的连接

    如上图: 创建一个---线程组----点击配置元件---添加FTP请求缺省值: IP:为你的FTP服务的IP remote file:为你FTP服务上的一个文件: Localfile:为你本地的路经: ...

  7. .net 代理类(WebService代理类的详解 )

    http://hi.baidu.com/654085966/item/53ee8c0f108ad78202ce1b1d   -----------转自 客户端调用Web Service的方式我现在知道 ...

  8. Linq to sql 之 事务

    描述:linq 是自带事物的.如果建了两个linq to sql 类,各自访问不同的数据库,事物如何保证呢. 验证:采用常用的COM+事务来验证一下是否有效. ServiceConfig config ...

  9. js 判断整数

    参考 https://blog.csdn.net/tangxiujiang/article/details/78073792 1.使用取余运算符(%) + 判断对象是否是数字来判断: 注意:空字符串. ...

  10. Idea设置类注释模板

    1.选择File–>Settings–>Editor–>File and Code Templates–>Includes–>File Header. 在Descript ...