SQLserver用游标实现循环 和 简单的量值函数
2016-02-20 23:26
260 查看
declare @sums numeric(15,2) -- 这里必须要写上位数,不然默认为整数 declare @cltno varchar(32) declare @feeMoney numeric(15,2) declare cur cursor for sELECT CltNo FROM memberinfo WHERE CltNo NOT IN ('00029098','00027565') -- 定义游标 select @sums = 0.0 open cur --打开游标 fetch next from cur into @cltno while @@fetch_status = 0 begin SELECT @feeMoney = fee FROM Ns_CMS_Gettable(@cltno, '2015-10-01', '22'); select @sums = @sums + @feeMoney; fetch next from cur into @cltno end close cur -- 关闭游标 Deallocate cur -- 删除游标 print @sums -- 输出 print cast(@sums as varchar(10)) + 'ddddddd' -- 把数字类型转换成字符串
函数
create FUNCTION [dbo].[ns_cms_getnewfeemoney](@cid int) returns numeric(15,2) as begin declare @sums numeric(15,2) declare @cltno varchar(32) declare @feeMoney numeric(15,2) declare cur cursor for sELECT CltNo FROM memberinfo WHERE CltNo NOT IN ('00029098','00027565') -- 定义游标 begin select @sums = 0.0 open cur fetch next from cur into @cltno while @@fetch_status = 0 begin SELECT @feeMoney = fee FROM Ns_CMS_Gettable(@cltno, '2015-10-01', '22'); select @sums = @sums + @feeMoney; fetch next from cur into @cltno end close cur -- 关闭游标 Deallocate cur -- 删除游标 end RETURN @sums end
相关文章推荐
- centos上安装及配置redis cluster
- 项目经理必备技能-数据库设计(好帖目录,持续更新)
- 数据库范式(1NF 2NF 3NF BCNF)详解
- Redis教程(十二):服务器管理命令总结
- Redis服务器的启动过程分析
- 让Redis在你的系统中发挥更大作用的几点建议
- 64位Windows下安装Redis教程
- NoSQL和Redis简介及Redis在Windows下的安装和使用教程
- CentOS Linux系统下安装Redis过程和配置参数说明
- oracle 表类型变量的使用
- 第一行代码-6.4 SQLite数据库存储
- oracle之函数使用大全
- 在业务系统中调用水晶报表提示:ORA-06502: PL/SQL: 数字或值错误 : 字符串缓冲区太小
- mysql忘记root密码
- mysql - 操作表
- 对MySQL加锁的初步理解(一)
- memcached 常用方法介绍
- Oracle中的NULL、’’(空字符串)以及’_’(空格)
- Memcached
- 在ubuntu下安装mysql