«

SQL游标用法

发布于 阅读:90


if OBJECT_ID('tempdb..#xxxx') is not null
drop table #xxxx
select (select count(*) from PDM_salesOrderDT2 where Doccode = w.refdoccode) as digit,0 as ti,
w.PrDtDate,w.DelDate,null as jztime,h.PostDate,WOOrder,refdoccode into #xxxx from PDM_WOMFGOrder w,PDM_sSODeliveryHD h where w.PrDtDate is not null and ISNULL(w.status,0) !=9 and h.DocCode  in (select DocCode from PDM_sSODeliveryDT where woorder = w.woorder)
--select * from #xxxx

declare mycur cursor  for select distinct refdoccode,digit from #xxxx 

open mycur
declare   @refdoccode    varchar(50)           
declare   @digit  int,@newtimes int
 fetch next from mycur into @refdoccode,@digit
 while @@FETCH_STATUS=0
 begin

    select @newtimes =times   from PDM_CopiesTime where @digit between copiesA and copiesB
    update #xxxx set ti = @newtimes  where refdoccode = @refdoccode

 fetch next from mycur into @refdoccode,@digit
 end
 close mycur
 DEALLOCATE mycur  
  --update #xxxx set voretime=case when isnull(postdate,'')='' then case when isnull(DATEDIFF(HOUR,endtime,getdate()),0)>0 then DATEDIFF(HOUR,endtime,getdate()) else 0 end else case when isnull(DATEDIFF(HOUR,endtime,postdate),0)>0 then DATEDIFF(HOUR,endtime,postdate) else 0 end end
 select * from #xxxx

SQL