微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQLserver用游标实现循环 和 简单的量值函数

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,'22'); 
    select  @sums = @sums + @feeMoney;
    fetch next  from cur into @cltno
    end 
    close cur                                                             -- 关闭游标
    Deallocate cur                                                        -- 删除游标
  end 
       RETURN @sums 
end 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐