sql sever 常见游标写法,及如何强制停止

declare @lot_msg varchar(255)      ,
      @ant int
/*select  @lot_msg =predoccode from propurdadiplanhd        
where  docdate>'2020-07-01'*/
      
 DECLARE cur1 CURSOR FOR select  predoccode from propurdadiplanhd        
where  docdate>@datetime1

OPEN cur1                                                                
  FETCH   NEXT  FROM cur1 INTO  @lot_msg    



   WHILE(@@FETCH_STATUS = 0 )                                                                
  BEGIN                   
     SET @ant = ISNULL(@ant,0) + 1          

declare @lot Nvarchar(1000)  declare @begin_date  datetime      
declare @time_date varchar(244)     
set @lot= ' SELECT  top 1 @begin_Date=begin_Date FROM OPENQUERY(orc_link'+','  +''''+'select * from ps_schedule_head where lot_no=' +''''+'''' +@lot_msg+''''+''''+    
''''+')'       
exec sp_executesql @lot ,N'@begin_date  datetime  output',    
@begin_date output    
print(@begin_date)  select   @time_date=   convert(datetime,replace(replace(replace(@begin_date,'年', '-'),'月','-'),'日',''))      
      
 update propurdadiplanitem  set      
 chengxinjindu =@time_date        
 from  propurdadiplanitem a inner join propurdadiplanhd  b on a.doccode =b.doccode 
 
 where isnull(chengxinjindu,'') !=''  and b.predoccode =@lot_msg 



IF  @ant  >= 1000                  
  BEGIN                  
     BREAK                   
  END    
    FETCH   NEXT  FROM  cur1  INTO @lot_msg                  
  END                  
  CLOSE  cur1                  
  DEALLOCATE  cur1           

红色部分为固定

黑色部分为变更内容 

原文地址:https://www.cnblogs.com/baili-luoyun/p/13358817.html