存储过程

  1. 一个简单存储过程例子
  2. CREATE PROCEDURE sp_cpsum_bylevel1 AS  
  3. begin  
  4.     declare @code varchar(50)  
  5.     declare @num varchar(50)  
  6.     declare @command varchar(1000)  
  7.     declare cursor_tmp_1 cursor for select left(ypcode,2),count(left(ypcode,2)) from cpname where ylevel=3  group by left(ypcode,2)  
  8.     open cursor_tmp_1  
  9.     fetch next from cursor_tmp_1 into @code, @num  
  10.     while @@fetch_status = 0  
  11.     begin     
  12.         set @command = 'update cpname set shuliang ='''+ @num + ''' where ycode ='''+ @code +''' and ylevel=1'  
  13.         print @command  
  14.         EXEC (@command)  
  15.         fetch next from cursor_tmp_1 into @code, @num         
  16.     end   
  17.     close cursor_tmp_1    
  18.     deallocate cursor_tmp_1  
  19. end  
  20.   
  21. GO  
  22. 执行 exec sp_cpsum_bylevel1 
原文地址:https://www.cnblogs.com/beijingstruggle/p/4904880.html