SqlServer如何使用cursor处理重复数据过程详解

我们孩还发多夫道知道了,就得看不我们后心回的 "家 ",不是起用看把一个有邮递区号邮差找得到的家,后心天能们后心回的 "家 ",不是空于而,风每都到小是一段时光。
/************************************************************ 
 * Code formatted by setyg 
 * Time: 2014/7/29 10:04:44 
 ************************************************************/ 
 
CREATE PROC HandleEmailRepeat 
AS  
DECLARE email CURSOR  
FOR 
  SELECT e.email 
     ,e.OrderNo 
     ,e.TrackingNo 
  FROM  Email20140725 AS e 
  WHERE e.[status] = 0 
  ORDER BY 
      e.email 
     ,e.OrderNo 
     ,e.TrackingNo 
 
BEGIN 
  DECLARE @@email       VARCHAR(200) 
      ,@firstEmail     VARCHAR(200) 
      ,@FirstOrderNO    VARCHAR(300) 
      ,@FirstTrackingNO   VARCHAR(300) 
      ,@NextEmail      VARCHAR(200) 
      ,@@orderNO      VARCHAR(300) 
      ,@NextOrderNO     VARCHAR(50) 
      ,@@trackingNO     VARCHAR(300) 
      ,@NextTrackingNO   VARCHAR(50) 
   
  BEGIN 
    OPEN email; 
    FETCH NEXT FROM email INTO @firstEmail,@FirstOrderNO, @FirstTrackingNO; 
    FETCH NEXT FROM email INTO @NextEmail,@NextOrderNO, @NextTrackingNO; 
    IF @NextEmail!=@firstEmail 
    BEGIN 
      INSERT INTO Email20140725Test 
       ( 
        email 
        ,OrderNo 
        ,TrackingNo 
       ) 
      VALUES 
       ( 
        @firstEmail 
        ,@FirstOrderNO 
        ,@FirstTrackingNO 
       );  
      SET @@email = @NextEmail; 
      SET @@orderNO = @NextOrderNO; 
      SET @@trackingNO = @NextTrackingNO; 
    END 
    ELSE 
    BEGIN 
      SET @@email = @NextEmail; 
      SET @@orderNO = @FirstOrderNO+'、'+@NextOrderNO; 
      SET @@trackingNO = @FirstTrackingNO+'、'+@NextTrackingNO; 
    END 
     
     
     
    FETCH NEXT FROM email INTO @NextEmail,@NextOrderNO,@NextTrackingNO 
    WHILE @@fetch_status=0 
    BEGIN 
      IF @NextEmail=@@email 
      BEGIN 
        IF (@NextOrderNO!=@@orderNO) 
          SET @@orderNO = @@orderNO+'、'+@NextOrderNO 
         
        PRINT 'orderNO:'+@@orderNO  
         
        IF (@@trackingNO!=@NextTrackingNO) 
          SET @@trackingNO = @@trackingNO+'、'+@NextTrackingNO 
         
        PRINT 'trackingNO:'+@@trackingNO 
      END 
      ELSE 
      BEGIN 
        INSERT INTO Email20140725Test 
         ( 
          email 
          ,OrderNo 
          ,TrackingNo 
         ) 
        VALUES 
         ( 
          @@email 
          ,@@orderNO 
          ,@@trackingNO 
         ); 
        SET @@email = @NextEmail; 
        SET @@orderNO = @NextOrderNO; 
        SET @@trackingNO = @NextTrackingNO; 
      END 
      FETCH NEXT FROM email INTO @NextEmail,@NextOrderNO, @NextTrackingNO; 
    END 
    CLOSE email; --关闭游标 
    DEALLOCATE email; --释放游标 
  END 
END

到此这篇关于SqlServer如何使用cursor处理重复数据过程详解就介绍到这了。避不开,那么改变它。更多相关SqlServer如何使用cursor处理重复数据过程详解内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
SQL SERVER临时表排序问题的如何解决方法

Vscode上如何使用SQL的方法

SQL Server 批量插入数据的完美如何解决方案

浅析SQL Server授予了CREATE TABLE权限但是无法创建表

group by 按某一时间段分组统计并查询(推荐)