游标嵌套(个人笔记)


create table #temp(tem_archno varchar(50),tem_pages int,tem_org int,tem_copy1 int)
declare @temp_archno varchar(50) 
declare @temp_pages int
declare @temp_org int
declare @temp_iscopy bit
declare @temp_iscopy1 int
                
declare mycursor cursor for select archno,archorgpages from archflow_t where

archbatchno='L20080312-01'order by archno 

open mycursor                  

fetch next from mycursor  into @temp_archno,@temp_pages  

while(@@fetch_status=0)   
begin
   set @temp_iscopy1=0
   declare mycursor1 cursor for select iscopy from imageproperty_t where archno=@temp_archno
   open mycursor1
   fetch next from mycursor1  into @temp_iscopy               

     
   while(@@fetch_status=0)
   begin
 
  if @temp_iscopy<>0
  set @temp_iscopy1=@temp_iscopy1+1
  fetch next from mycursor1 into @temp_iscopy
   end
   set @temp_org=@temp_pages-@temp_iscopy1
   insert into #temp values(@temp_archno,@temp_pages,@temp_org,@temp_iscopy1)
   fetch next from mycursor into @temp_archno,@temp_pages
   close mycursor1  
   deallocate mycursor1
end

close mycursor


select * from #temp order by tem_archno

你可能感兴趣的:(游标)