sql server 的存储过程实例

/**********
实现功能,将项目每年的月份中的资金数据累加
使用了三个循环嵌套游标,可能性能不是很好,主要演示存储过程的实现
***************/

CREATE  PROCEDURE accumulate
AS
declare @temp varchar(255)
declare @Field int

declare @practice_all int
declare @capatital_all int
declare @temppractice int
declare @tempcapital int

declare @month int
declare @year int

declare prject_cursor cursor for SELECT [projectid] AS projectid FROM v_project_year_month GROUP BY projectid

set @practice_all=0
set @capatital_all=0

open prject_cursor    --打开游标
fetch next from prject_cursor into @Field
while(@@fetch_status=0)    --循环开始
begin
    declare year_cursor cursor for SELECT [year_yd] FROM v_project_year_month WHERE (projectid = @Field) GROUP BY year_yd
    open year_cursor
    fetch next from year_cursor into @year
    while(@@fetch_status=0)
    begin
        print cast(@year as varchar)+'年'+cast(@Field as varchar)+'============项目'
        declare month_cursor cursor for SELECT [Month_yf],[Month_practice],[Month_capital] FROM v_project_year_month WHERE (year_yd=@year) and (projectid =@Field) ORDER BY Month_yf
        open month_cursor
        fetch next from month_cursor into @month,@temppractice,@tempcapital
        while(@@fetch_status=0)
        begin
            
            set @temp=cast(@month as varchar)
            print '月份--'+@temp
            print '投资:'+cast(@temppractice as varchar)+'资金:'+cast(@tempcapital as varchar)
            set @practice_all=@practice_all+@temppractice
            set @capatital_all=@capatital_all+@tempcapital
            --累计投资
            Exec('update v_project_year_month set Month_practice_total='+@practice_all+' where projectid='+@Field+' and month_yf='+@month+'')
            --累计资金
            Exec('update v_project_year_month set Month_capital_total='+@capatital_all+' where projectid='+@Field+' and month_yf='+@month+'')
            
            print @practice_all
            print @capatital_all
            fetch next from month_cursor into @month,@temppractice,@tempcapital
        end
        close month_cursor    --关闭游标
        deallocate month_cursor--释放资源
    fetch next from year_cursor into @year
    end
    close year_cursor    --关闭游标
    deallocate year_cursor    --释放资源
fetch next from prject_cursor into @Field --下个prject
set @practice_all=0    --归零
set @temppractice=0    --归零
set @capatital_all=0    --归零
set @tempcapital=0    --归零
end
close prject_cursor    --关闭游标
deallocate prject_cursor--释放资源

GO
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值