步步为营101-同一个PCode下重复的OrderNumber重新排序
Posted 逍遥小天狼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了步步为营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
以上是关于步步为营101-同一个PCode下重复的OrderNumber重新排序的主要内容,如果未能解决你的问题,请参考以下文章
牛客top101题---删除有序链表中重复的元素-II (java)