1. 程式人生 > >步步為營101-同一個PCode下重復的OrderNumber重新排序

步步為營101-同一個PCode下重復的OrderNumber重新排序

end lin 過程 closed alt procedure _id var bpm

技術分享圖片

技術分享圖片

技術分享圖片
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重新排序