【APT】SqlServer游标使用
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游标使用的更多相关文章
- Sqlserver游标复习
经常写存储过程,但今天在游标使用过程中还是疏忽了一些事情,执行过程中一直执行不下去,后来直接sqlserver挂了,教训啊! 代码虽简单,望铭记: Create PROCEDURE [dbo].[te ...
- SqlServer游标的创建与使用
前言 大家都对SqlServer视图.存储过程.触发器的创建与使用有一定的了解了,我们来看下什么是游标,怎么使用,什么时候用. SqlServer视图的创建与使用 SqlServer存储过程的创建与使 ...
- sqlserver 游标的使用
declare @temp_temp uniqueidentifier--临时变量 DECLARE aaa CURSOR for select Id from A ------------------ ...
- SqlServer游标简介
游标实例: Declare MyCusror Cursor Scroll For Select * From Master_Goods Order By GoodsID Ope ...
- sqlserver 游标
DECLARE ChangeInvCodeCursor CURSOR FOR SELECT A.name AS tablecolumn,C.name AS tablename FROM sys.col ...
- SQLServer游标(Cursor) (B)
游标(Cursor)是处理数据的一种方法,为了查看或者处理结果集中的数据,游标提供了在结果集中一次以行或者多行前进或向后浏览数据的能力.我们可以把游标当作一个指针,它可以指定结果中的任何位置,然后允许 ...
- SQLServer 游标 (A)
游标 游标分为客户端游标和服务器端游标.Sql通过游标可以对一个结果集进行逐行处理.对于使用服务器端游标的过程有:声明.打开.读取.关闭.释放. 1 声明游标 1.1 SQL-92标准的声明 Decl ...
- SQLserver游标原理和使用方法
在数据库开发过程中,当你检索的数据只是一条记录时,你所编写的事务语句代码往往使用SELECT INSERT 语句.但是我们常常会遇到这样情况,即从某一结果集中逐一地读取一条记录.那么如何解决这种问题呢 ...
- (转)sqlserver游标概念与实例全面解说
首先声明:该文章转自http://www.cnblogs.com/wudiwushen/archive/2010/03/30/1700925.html 的博客 引言 我们先不讲游标的什么概念,步骤 ...
随机推荐
- How to Pronounce the Idiom: ‘Out Like a Light’
How to Pronounce the Idiom: ‘Out Like a Light’ Share Tweet Share Tagged With: Idioms English is full ...
- js版RSA算法
// RSA, a suite of routines for performing RSA public-key computations in// JavaScript.//// Requires ...
- 浏览器唤起APP的思路(本文转载)
在做 h5 页面中,会遇到这样一个需求,有一个立即打开的按钮,如果本地安装了我们的 app,那么点击就直接唤起本地 app,如果没有安装,则跳转到下载. 首先想到的是两个问题:一是如何唤起本地 app ...
- java程序员到底该不该了解一点算法(一个简单的递归计算斐波那契数列的案例说明算法对程序的重要性)
为什么说 “算法是程序的灵魂这句话一点也不为过”,递归计算斐波那契数列的第50项是多少? 方案一:只是单纯的使用递归,递归的那个方法被执行了250多亿次,耗时1分钟还要多. 方案二:用一个map去存储 ...
- Mac中使用pycharm引包matplotlib失败
最开始是使用matplotlib这个包,然后在pycharm中失败,然后在终端中pip install matplotlib,发现,安装了以后,pycharm依然找不到包. 代码如下: import ...
- pl2303 驱动
https://blog.csdn.net/ouening/article/details/70947759
- UMD模式的js
(function (root, factory) { if (typeof define === 'function' && define.amd) { // AMD define( ...
- linux mysqlERROR 1045 (28000): linux忘记数据库密码
已验证没问题 #1.停止mysql数据库(确定能停止掉,不然第二部有问题) /etc/init.d/mysqld stop #2.执行如下命令 mysqld_safe --user=mysql - ...
- mybatis做if 判断 传入值0 建议最好不要使用值0
mybatis做if 判断 注意:下面这种写法只适用于 id 类型为字符串. <if test="id != null and id != '' "> id = ...
- canvas动画---- 太阳、地球、月球
<div> <canvas id="canvas" width="500" height="500"></ca ...