循环处理--sqlserver

时间:2021-05-22 04:27:11

alter PROCEDURE [dbo].[sp_gongzi] @gongzi_yf varchar(7) as
DECLARE  @input_id varchar(20)
DECLARE  @sk_sum decimal(18, 3)
DECLARE  @sk_sum_yk decimal(18, 3)
BEGIN 
    DECLARE cur_input CURSOR  FOR
            select docno from  tc_tab1 where yf = @gongzi_yf    
 
 OPEN cur_input
 
    FETCH  cur_input INTO @input_id
 WHILE @@FETCH_STATUS = 0
      BEGIN
         set @sk_sum = 0    
         set @sk_sum_yk  = 0
         select @sk_sum =  sum(je * case when (hl2 is null) then hl else hl2 end )         
             from sk_dan where docno =@input_id;
        
         select @sk_sum_yk =  sum(je * case when (hl2 is null) then hl else hl2 end )         
             from sk_dan_yk where docno =@input_id;       
               
        set @sk_sum = case when (@sk_sum is null) then 0 else @sk_sum end;       
        set @sk_sum_yk =  case when (@sk_sum_yk is null) then 0 else @sk_sum_yk end;       
       
        update    tc_tab1 set dke2 =  @sk_sum   +   @sk_sum_yk
     where docno = @input_id; 
       
   FETCH  cur_input INTO @input_id
      END
    CLOSE cur_input
    DEALLOCATE cur_input 
    --*/
END