-- =============================================

-- Author:                tanghong

-- Create date: 20130628154520

-- =============================================

CREATE PROCEDURE [dbo].[PersonalPrizeCount]

@EndDate datetime,

@iGroup int,

@TradeSum money out,

@commission money out,

@realsum money out

AS

BEGIN

SET NOCOUNT ON;

 
 

set @TradeSum = 0

set @commission = 0

set @realsum = 0

 
 

declare @EquiNO int

--declare @prize int

declare equipment_cursor SCROLL CURSOR FOR

select EquiNo from Equipment where groupno in ( select id from GetGroupTreeAllSons(@iGroup) ) for read only

 
 

open equipment_cursor

fetch from equipment_cursor into @EquiNO

while @@fetch_status=0

begin

declare @tempsum money

declare @tempcommsion money

declare @temprealsum money

 
 

declare @LastTradeID int

declare @newLastTradeid int

 
 

select @LastTradeID = isnull(MAX(LastTradeID),0) from statPrizeCount where EquiNo = @EquiNo

 
 

select @tempsum = isnull(sum(tradesum),0), @newLastTradeid = ISNULL(max(tradeid), 0) from tradelist where equipmentno = @EquiNo and tradeid > @LastTradeID and referdate < @EndDate

set @tradesum = @tempsum + @tradesum

 
 

declare @money0 money, @money1 money, @money2 money, @money3 money, @money4 money, @money5 money

declare @prize0 float, @prize1 float, @prize2 float, @prize3 float, @prize4 float, @prize5 float

 
 

select @money0 = isnull(money0,0), @prize0 = isnull(sameMoney,0),

@money1 = isnull(money,0), @prize1 = isnull(prize,0),

@money2 = isnull(money2,0), @prize2 = isnull(prize2,0),

@money3 = isnull(money3,0), @prize3 = isnull(prize3,0),

@money4 = isnull(money4,0), @prize4 = isnull(prize4,0),

@money5 = isnull(money5,0), @prize5 = isnull(prize5,0)

from prize where groupid = @iGroup

 
 

if @tempsum <= @money0

set @tempcommsion = @prize0

else if @tempsum <= @money1

set @tempcommsion = @tempsum * @prize1 / 100

else if @tempsum <= @money2

set @tempcommsion = @tempsum * @prize2 / 100

else if @tempsum <= @money3

set @tempcommsion = @tempsum * @prize3 / 100

else if @tempsum <= @money4

set @tempcommsion = @tempsum * @prize4 / 100

else if @tempsum <= @money5

set @tempcommsion = @tempsum * @prize5 / 100

else

set @tempcommsion = 0

 
 

set @commission = @tempcommsion + @commission

set @temprealsum = @tempsum - @tempcommsion

 
 

declare @ddd datetime;

select @ddd = EndDate from statprizecount where LastTradeID = @LastTradeID and EquiNo = @EquiNo

 
 

insert into statprizecount(EquiNo, EndDate, TradeSum, Commsion, RealSum, LastTradeID, OperTime, startDate)

values(@EquiNO, @EndDate, @tempsum, @tempcommsion, @temprealsum, @newLastTradeid, GETDATE(), @ddd)

 
 

print @EquiNO

fetch from equipment_cursor into @EquiNO

end

 
 

close equipment_cursor

deallocate equipment_cursor

set @realsum = @TradeSum - @commission

END

 
 

GO

 
 

调用代码

 
 

double t1,t2,t3;

strSql.Format(_T("declare @t1 money, @t2 money, @t3 money EXECUTE PersonalPrizeCount '%s',%d,@t1 out, @t2 out, @t3 out select 't1' = @t1, 't2' = @t2, 't3' = @t3"), LPCTSTR(strDtTo), m_iGroup);

CADORecordset rst(&CGenericBasic::m_DataBase);

 
 

rst.Open(LPCTSTR(strSql));

if(!rst.IsEOF())

{

rst.GetFieldValue(0, t1);

rst.GetFieldValue(1, t2);

rst.GetFieldValue(2, t3);

}

 
 

 
 

create procedure [dbo].[ComputeRefund]

@accountno int,

@subsidyrefund money out,

@cashrefund money out

as

begin

set nocount on;

set @cashrefund = 0

set @subsidyrefund = 0

 
 

--删除临时表

if OBJECT_ID('tempdb..#aaa') is not null Begin

drop table #aaa

end

 
 

SELECT

IDENTITY(INT,1,1) as seq,--添加自增列

CONVERT(CHAR(7), DATETIME, 120) AS 月份,

ACCOUNTNO,

cast(0 as money) as 原补助结余,

cast(0 as money) as 原现金结余,

SUM(CASE TRADETYPE WHEN 1 THEN TRADESUM WHEN 2 THEN TRADESUM ELSE 0 END) AS 充值,

SUM(CASE TRADETYPE WHEN 5 THEN TRADESUM ELSE 0 END) AS 补助,

SUM(CASE TRADETYPE WHEN 132 THEN TRADESUM ELSE 0 END) AS 消费,

cast(0 as money) as 补助结余,

cast(0 as money) as 现金结余

into #aaa

FROM TradeList

WHERE (ACCOUNTNO = @accountno)

GROUP BY CONVERT(CHAR(7), DATETIME, 120), ACCOUNTNO        

ORDER BY ACCOUNTNO

 
 

declare per_cursor cursor for select seq, 原补助结余, 原现金结余, 充值, 补助, 消费, 补助结余, 现金结余 from #aaa

declare @seq int, @原补助结余 money, @原现金结余 money, @充值 money, @补助 money, @消费 money, @补助结余 money, @现金结余 money

 
 

OPEN per_cursor

FETCH NEXT FROM per_cursor INTO @seq, @原补助结余, @原现金结余, @充值, @补助, @消费, @补助结余, @现金结余

 
 

WHILE @@FETCH_STATUS = 0

BEGIN

--print @seq

if (@消费 <= (@原补助结余+@补助)) begin

set @补助结余 = (@原补助结余+@补助-@消费)

set @现金结余 = (@原现金结余+@充值)

end

else begin

set @补助结余 = 0

set @现金结余 = (@原补助结余+@原现金结余+@充值+@补助-@消费)

end

--print @现金结余

update #aaa set 补助结余 = @补助结余, 现金结余 = @现金结余 where seq = @seq

 
 

IF exists (SELECT seq from #aaa where seq = @seq + 1)

update #aaa set 原补助结余 = @补助结余, 原现金结余 = @现金结余 where seq = @seq + 1

 
 

FETCH NEXT FROM per_cursor INTO @seq, @原补助结余, @原现金结余, @充值, @补助, @消费, @补助结余, @现金结余        

END

 
 

CLOSE per_cursor

DEALLOCATE per_cursor

 
 

set @subsidyrefund = @补助结余

set @cashrefund = @现金结余

 
 

end

 
 

SQL server 常见用法记录的更多相关文章

  1. SQL SERVER常见等待——解决会话等待产生的系统问题

    SQL SERVER——解决会话等待产生的系统问题 转自: https://blog.csdn.net/z_cloud_for_SQL/article/details/55051215 版权声明:SQ ...

  2. SQL Server常见数据类型介绍

    数据表是由多个列组成,创建表时必须明确每个列的数据类型,以下列举SQL Server常见数据类型的使用规则,方便查阅. 1.整数类型 int 存储范围是-2,147,483,648到2,147,483 ...

  3. SQL Server 查询表的记录数(3种方法,推荐第一种)

    http://blog.csdn.net/smahorse/article/details/8156483 --SQL Server 查询表的记录数 --one: 使用系统表. SELECT obje ...

  4. 【转】SQL Server 查询表的记录数(3种方法,推荐第一种)

    --SQL Server 查询表的记录数 --one: 使用系统表. SELECT object_name (i.id) TableName, rows as RowCnt FROM sysindex ...

  5. SQL Server 常见数据类型介绍

    数据表是由多个列组成,创建表时必须明确每个列的数据类型,以下列举SQL Server常见数据类型的使用规则,方便查阅. 整数类型 int 存储范围是-2,147,483,648到2,147,483,6 ...

  6. SQL Server 最小日志记录

    SQL Server之所以记录事务日志,首要目的是为了把失败或取消的操作还原到最原始的状态,但是,并不是所有的操作都需要完全记录事务日志,比如,在一个空表上放置排他锁,把大量的数据插入到该空表中.即使 ...

  7. sql server 常见错误代码15000 - 15999含义解析

    错误 15000 - 15999 SQL Server 2008 R2 其他版本 错误 严重性 是否记录事件 说明(消息正文) 15001 16 否 对象 '%ls' 不存在或不是此操作的有效对象. ...

  8. SQL Server常见基础操作

    1. 常见针对表的操作(增删改查) --1. Create Table USE [MVC_000] CREATE TABLE T_TableName ( ID ,) PRIMARY KEY, Name ...

  9. SQL Server 删除重复记录,只保留一条记录

    原文地址:http://blog.csdn.net/eriato/article/details/17417303 有张表格之前没有设计关键字段的唯一约束,导致有时候执行插入操作时不小心执行了多次就出 ...

随机推荐

  1. Android播播放完SD卡指定文件夹音乐之后,自动播放下一首

    最近做一个项目,需要连续播放音乐,播放完一首歌之后,自动播放完下一首歌.不要重复播放. 代码如下: package com.example.asyncplayer_ex; import java.io ...

  2. [AngularJS] Lazy loading Angular modules with ocLazyLoad

    With the ocLazyLoad you can load AngularJS modules on demand. This is very handy for runtime loading ...

  3. iOS开发——实用篇Swift篇&状态栏操作

    状态栏操作 在Swift开发过程中,针对状态栏操作的过程有很多. 1.在ViewController中操作当前ViewController的状态栏 /** 隐藏状态栏 */ override func ...

  4. quickstack is a tool to take call stack

    https://github.com/yoshinorim/quickstack quickstack is a tool to take call stack traces with minimal ...

  5. Qt focusoutevent 不响应的解决方法

    一般利用focus(焦点)来实现弹窗自动关闭效果. Qt的focus貌似是自己的bug, 经常无法接收到focusout的事件 例如: widgetA 中执行  widgetB->show(); ...

  6. BestCoder Sequence

    hdu  4908  Bestcoder Problem Description Mr Potato is a coder.Mr Potato is the BestCoder. One night, ...

  7. Android 高级UI设计笔记04:使用setDrawingCacheEnabled(boolean flag)提高绘图速度

    1. View组件显示的内容可以通过cache机制保存为bitmap, 使用到的API有: void setDrawingCacheEnabled(boolean flag) Bitmap getDr ...

  8. struts2.1笔记03:AOP编程和拦截器概念的简介

    1.AOP编程 AOP编程,也叫面向切面编程(也叫面向方面):Aspect Oriented Programming(AOP),是目前软件开发中的一个热点,也是Spring框架中的一个重要内容.利用A ...

  9. Jersey(1.19.1) - Client API, Using filters

    Filtering requests and responses can provide useful functionality that is hidden from the applicatio ...

  10. HttpClient(4.3.5) - HTTP Header

    An HTTP message can contain a number of headers describing properties of the message such as the con ...