SQL server 常见用法记录
-- =============================================
-- 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 常见用法记录的更多相关文章
- SQL SERVER常见等待——解决会话等待产生的系统问题
SQL SERVER——解决会话等待产生的系统问题 转自: https://blog.csdn.net/z_cloud_for_SQL/article/details/55051215 版权声明:SQ ...
- SQL Server常见数据类型介绍
数据表是由多个列组成,创建表时必须明确每个列的数据类型,以下列举SQL Server常见数据类型的使用规则,方便查阅. 1.整数类型 int 存储范围是-2,147,483,648到2,147,483 ...
- SQL Server 查询表的记录数(3种方法,推荐第一种)
http://blog.csdn.net/smahorse/article/details/8156483 --SQL Server 查询表的记录数 --one: 使用系统表. SELECT obje ...
- 【转】SQL Server 查询表的记录数(3种方法,推荐第一种)
--SQL Server 查询表的记录数 --one: 使用系统表. SELECT object_name (i.id) TableName, rows as RowCnt FROM sysindex ...
- SQL Server 常见数据类型介绍
数据表是由多个列组成,创建表时必须明确每个列的数据类型,以下列举SQL Server常见数据类型的使用规则,方便查阅. 整数类型 int 存储范围是-2,147,483,648到2,147,483,6 ...
- SQL Server 最小日志记录
SQL Server之所以记录事务日志,首要目的是为了把失败或取消的操作还原到最原始的状态,但是,并不是所有的操作都需要完全记录事务日志,比如,在一个空表上放置排他锁,把大量的数据插入到该空表中.即使 ...
- sql server 常见错误代码15000 - 15999含义解析
错误 15000 - 15999 SQL Server 2008 R2 其他版本 错误 严重性 是否记录事件 说明(消息正文) 15001 16 否 对象 '%ls' 不存在或不是此操作的有效对象. ...
- SQL Server常见基础操作
1. 常见针对表的操作(增删改查) --1. Create Table USE [MVC_000] CREATE TABLE T_TableName ( ID ,) PRIMARY KEY, Name ...
- SQL Server 删除重复记录,只保留一条记录
原文地址:http://blog.csdn.net/eriato/article/details/17417303 有张表格之前没有设计关键字段的唯一约束,导致有时候执行插入操作时不小心执行了多次就出 ...
随机推荐
- Tomcat7集群扩展session集中管理,tomcat-redis-session-manager使用
请参考官方文档 下载所需的包了: tomcat-redis-session-manager-1.1.jar jedis-2.1.0.jar commons-pool-1.6.jar 将这些jar包都丢 ...
- UNIX基础知识之文件和目录
程序清单1-1 列出一个目录中的所有文件(ls命令的简要实现): [root@localhost unix_env_advance_prog]# cat prog1-.c #include " ...
- java_io_操作封装
package com.wiker; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import j ...
- 小米2s使用Hexamob Recovery PRO恢复数据
这东西对于手机来说, 真是神器啊 现在很多手机都是以MTP连接到电脑的, 所以在PC上是看不到盘符, 也就无法使用finaldata 之类的工具恢复了. 而像小米2S这样的手机, 无法外接SD卡, 则 ...
- zookeeper-3.4.6安装
下载zookeeper包并解压 配置.在conf文件夹中将zoo_sample.cfg复制一份为zoo.cfg 修改zoo.cfg 在/home/admln/zookeeper中新建一个文件myid( ...
- qsort,mergesort,插入排序
//插入排序 int a[n]; ;i<=n;i++) { int s=a[i]; ; while(j&&a[j]>a[i]) { a[j+]=a[j]; j--; } a ...
- 使用JS制作一个鼠标可拖的DIV(三)——移动带图片DIV
当DIV元素里,存在图片元素的时候,会使拖动出现异常. 因为图片元素本身就支持拖动,所以,只要在图片标签加入:draggable='false'. 如下: <div onmousedown=&q ...
- 程氏CMS去掉静态页面的隐藏性版权方法
程氏CMS去掉静态页面的隐藏性版权方法 实例如图: 因为之前自己找了好久都没找到这俩代码写在那个文件夹的,经过跟csqq8讨论了也没有得到结果,今天突然发现,原来这些代码都经过base64加密,用加密 ...
- [改善Java代码]让工具类不可实例化
建议42: 让工具类不可实例化 Java项目中使用的工具类非常多,比如JDK自己的工具类java.lang.Math.java.util.Collections等都是我们经常用到的.工具类的方法和属性 ...
- 【欧拉定理】计算(a^(b^c))%1000000007
欧拉定理(称费马-欧拉定理或欧拉 函数定理) 欧拉定理表明,若n,a为正整数,且n,a互素(即gcd(a,n)=1),则 这个定理可以用来简化幂的模运算.比如计算7222的个位数,实际是求7222被1 ...