步步为营101-同一个PCode下重复的OrderNumber重新排序

USE [K2_WorkFlow_Test]
GO
/****** Object:  StoredProcedure [dbo].[sp_UpdateBPM_DictionaryForOrderNumber]    Script Date: 2018/1/4 16:08:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[sp_UpdateBPM_DictionaryForOrderNumber]
  --@row  int output --受影响行数
as
begin 
  --01 捕获异常
  begin try
     -- 创建临时表,用于存储"字典表"中的OrderNumber重复的父编码(Pcode)
     IF OBJECT_ID('tempdb.dbo.#devicetemp','U') IS NOT NULL DROP TABLE dbo.#devicetemp; 
     SELECT Pcode 
     INTO dbo.#devicetemp
    from BPM_Dictionary_Test         --注意,这里先拿测试表做测试,上线时换成正式表
    Group by Pcode,OrderNumber having count(Pcode) >1
     -- 声明变量
     DECLARE
         @Pcode nvarchar(50)  
         
     WHILE EXISTS(SELECT Pcode FROM dbo.#devicetemp)
     BEGIN
         SELECT @Pcode=Pcode FROM dbo.#devicetemp;
          
            -- 创建临时表,用于存储"字典表"中当前父编码(Pcode)重复的详细信息
            IF OBJECT_ID('tempdb.dbo.#repeatDetailInfotemp','U') IS NOT NULL DROP TABLE dbo.#repeatDetailInfotemp;  
            begin transaction         
                 SELECT Id,OrderNumber,Pcode    
                 INTO dbo.#repeatDetailInfotemp
                 FROM  BPM_Dictionary_Test where Pcode=@Pcode
                 ORDER BY OrderNumber desc;
                  -- 声明变量
                  DECLARE
                    @Id AS INT,
                    @OrderNumber int
                    set @OrderNumber=0
                 WHILE EXISTS(SELECT Id FROM dbo.#repeatDetailInfotemp)
                 BEGIN
                   SELECT @Id=Id FROM dbo.#repeatDetailInfotemp;
                   UPDATE BPM_Dictionary_Test set OrderNumber=@OrderNumber  where Id = @Id;
                   set  @OrderNumber = @OrderNumber +1;
                   SET ROWCOUNT 0         
                   DELETE FROM dbo.#repeatDetailInfotemp WHERE Id=@Id;
                 END
            commit transaction            
            
         SET ROWCOUNT 0
         
         DELETE FROM dbo.#devicetemp WHERE Pcode=@Pcode;         
     END 

     --set @row = @@rowcount
    end try     
    BEGIN catch
        rollback transaction
       -- set @IsSuccess = '失败!'
    END catch
    
end
存储过程

 

posted @ 2018-01-04 16:25  逍遥小天狼  阅读(249)  评论(0编辑  收藏  举报