declare @UserID int
declare DZcursor CURSOR for select UserID from Z_Score 
open DZcursor      
fetch next from DZcursor into @UserID
while @@fetch_status=0
beginprint @UserIDfetch next from DZcursor into @UserID 
end
close DZcursor
deallocate DZcursor/*  打印出的结果1
2
3
4
5
6
7
8
9
10
*//*  我想要 得出的结果 是  这些值 相加 55*/   
   有没有 哪个 知道 该怎么做啊? 

解决方案 »

  1.   

    declare @UserID int
    declare @result int
    declare DZcursor CURSOR for select UserID from Z_Score 
    open DZcursor      
    fetch next from DZcursor into @UserID
    while @@fetch_status=0
    begin  set @result = @result + @useridfetch next from DZcursor into @UserID 
    end
    close DZcursor
    deallocate DZcursorprint @result
      

  2.   

    declare @UserID int
    declare @result int
    set @result = 0
    declare DZcursor CURSOR for select UserID from Z_Score 
    open DZcursor      
    fetch next from DZcursor into @UserID
    while @@fetch_status=0
    begin  set @result = @result + @useridfetch next from DZcursor into @UserID 
    end
    close DZcursor
    deallocate DZcursorprint @result
      

  3.   


    我这样写 declare @UserID int
    declare @result int
    set @result = 0
    declare DZcursor CURSOR for select UserID from Z_Score 
    open DZcursor      
    fetch next from DZcursor into @UserID
    while @@fetch_status=0
    begin  set @result = @result + @userid
    print @resultfetch next from DZcursor into @UserID 
    end
    close DZcursor
    deallocate DZcursor/*   打印的结果为
    13
    44
    87
    92
    149
    154
    158
    166
    174
    */我只想要 最后一条结果  
    /*
    174
    */
    能不能 做出来啊?
      

  4.   

    select sum(UserID) from Z_Score 
    直接这样一句就可以了,用游标不蛋痛吗?
      

  5.   

    你如果想要最后一个结果,
    你把那个print语句放到循环的外面就可以了,为什么一定要放到循环里面去
      

  6.   

    那好,  我将我  完整的 语句 贴出来 ,  不知道 你能不能 看明白, 我的问题 就在那里面 declare @UserID int,@h int,@j int,@a numeric(18,2), @b numeric(18,2), @c numeric(18,2) 
    declare DZcursor CURSOR for select distinct UserID from Z_Score where item_check in ('001考勤积分','002订单数积分','003前单积分')
    open DZcursor      
    fetch next from DZcursor into @UserID
    while @@fetch_status=0
    begin  ---------------
    select @h=count(distinct UserID) from Z_Score where item_check in ('001考勤积分','002订单数积分','003前单积分')
    select @j=count(*) from  Z_Score  where item_check in ('000人均效能积分') and MMYY=convert(varchar(7),getdate(),120)select @a=convert(numeric(18,2),Stand_Score*0.05)   ---------------
    from Z_Score where item_check = '001考勤积分' and UserID=@UserID select @b=convert(numeric(18,2),Stand_Score*0.20)   ---------------
    from Z_Score where item_check = '002订单数积分' and UserID=@UserID select @c=convert(numeric(18,2),Stand_Score*0.10)   ---------------
    from Z_Score where item_check = '003前单积分' and UserID=@UserID 
    insert into Z_Score  
    select convert(varchar(7),getdate(),120),@UserID,
    '000效能积分',(@a+@b+@c),0,0,0,0,datepart(d,getdate()),(@a+@b+@c)/100insert into Z_Score  
    select convert(varchar(7),getdate(),120),@UserID,
    '000效能级别',(@a+@b+@c)/((@a+@b+@c)/@h),0,0,0,0,datepart(d,getdate()),0if @j=0
    begin
    insert into Z_Score  
    select convert(varchar(7),getdate(),120),14,
    '000人均效能积分',(@a+@b+@c)/@h,0,0,0,0,datepart(d,getdate()),0
    end/*  问题就在于   @a+@b+@c   得出的结果是       
    13.78
    31.11
    43.15
    5.44
    57.35
    5.12
    4.10
    8.96
    8.17
    */
    我要的结果是  打印结果 相加之后 的结果  求和 
    fetch next from DZcursor into @UserID  
    end
    close DZcursor
    deallocate DZcursor