Calling: ----------------------------------------- USE [db02_MelaEmp] GO DECLARE @return_value int EXEC @return_value = [dbo].[SPRefreshTProgramFPos] @psSystemID = N'MELAEMP', @piParent = -1 SELECT 'Return Value' = @return_value GO Sample Table: --------------------------------------- USE [db02_MelaEmp] GO /****** Object: Table [dbo].[TPrograms] Script Date: 03/06/2007 20:47:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[TPrograms]( [FSystemID] [nvarchar](20) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL, [FProgramNo] [int] NOT NULL, [FParentNo] [int] NULL, [FSequence] [int] NULL, [FType] [nvarchar](1) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL, [FProgramID] [nvarchar](20) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL, [FTreeLevel] [int] NULL, [FTreeDegree] [int] NULL, [FTreeLeaf] [int] NULL, [FCaption] [nvarchar](50) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL, [FPath] [nvarchar](50) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL, [FUpdateTime] [datetime] NULL CONSTRAINT [DF_TPrograms_FUpdateTime] DEFAULT (getdate()), [FCreateTime] [datetime] NULL CONSTRAINT [DF_TPrograms_FCreateTime] DEFAULT (getdate()), CONSTRAINT [PK_TPrograms] PRIMARY KEY CLUSTERED ( [FSystemID] ASC, [FProgramNo] ASC ) ON [PRIMARY] ) ON [PRIMARY] Code: --------------------------------------- USE [db02_MelaEmp] GO /****** Object: StoredProcedure [dbo].[SPRefreshTProgramFPos] Script Date: 03/06/2007 20:46:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[SPRefreshTProgramFPos] (@psSystemID char(20), @piParent int) AS -- Refresh TPrograms.FPosY, FPosX SET NOCOUNT ON -- TProgram.Fields DECLARE @iFProgramNo int DECLARE @iFTreeLevel int DECLARE @iFParentNo int DECLARE @iFSequence int -- Tree base DECLARE @iLevel int, @iDegree int, @iLeaf int -- Temp Variables. DECLARE @bSameBlock_Level int DECLARE @bSameBlock_Degree int DECLARE @iBlock_Level int DECLARE @iBlock_ParentNo int DECLARE @s1 char(30) DECLARE @i1 int DECLARE @sTrace char(80) DECLARE @iLoop1 int DECLARE @iLoop2 int DECLARE @iLoop3 int set @iLoop1=0 -- Reset tree indexes update TPrograms set FTreeLevel = -1, FTreeDegree = -1, FTreeLeaf = -1 where FSystemID = @psSystemID -- Reset Tree Level CREATE TABLE #stack (FItem int, FLevel int) INSERT INTO #stack VALUES (@piParent, 1) set @iLevel = 1 WHILE @iLevel > 0 BEGIN IF EXISTS (SELECT * FROM #stack WHERE FLevel = @iLevel) BEGIN SELECT @piParent = FItem FROM #stack WHERE FLevel = @iLevel set @iLoop1 = @iLoop1 + 1 set @i1 = @iLevel - 1 SELECT @sTrace = cast(@i1 as char(4)) + space(@i1) + cast(@piParent as char(10)) PRINT @sTrace update TPrograms set FTreeLevel = @i1 where FSystemID = @psSystemID and FProgramNo = @piParent DELETE FROM #stack WHERE FLevel = @iLevel AND FItem = @piParent INSERT #stack SELECT FProgramNo, @iLevel + 1 FROM TPrograms where FSystemID = @psSystemID and FParentNo = @piParent ORDER BY FSequence desc, FProgramNo desc IF @@ROWCOUNT > 0 BEGIN set @iLevel = @iLevel + 1 END END ELSE set @iLevel = @iLevel - 1 END -- WHILE -- Reset Tree degree and leaf DECLARE cus1 CURSOR FOR SELECT FTreeLevel, FParentNo, FSequence, FProgramNo FROM TPrograms WHERE FSystemID = @psSystemID ORDER BY FTreeLevel, FParentNo, FSequence, FProgramNo OPEN cus1 FETCH NEXT FROM cus1 INTO @iFTreeLevel, @iFParentNo, @iFSequence, @iFProgramNo set @iLoop1 = 1 set @iLoop2 = 1 set @iLoop3 = 0 set @iLeaf = 0 set @iDegree = 1 set @iBlock_Level = @iFTreeLevel set @iBlock_ParentNo = @iFParentNo WHILE @@FETCH_STATUS = 0 BEGIN set @bSameBlock_Level = 1 set @bSameBlock_Degree = 1 IF @iBlock_ParentNo <> @iFParentNo BEGIN set @bSameBlock_Degree = 0 set @iBlock_ParentNo = @iFParentNo END IF @iBlock_Level <> @iFTreeLevel BEGIN set @bSameBlock_Level = 0 set @bSameBlock_Degree = 0 set @iBlock_ParentNo = @iFParentNo set @iBlock_Level = @iFTreeLevel END IF @bSameBlock_Level = 1 BEGIN IF @bSameBlock_Degree = 1 BEGIN set @iLoop3 = @iLoop3 + 1 set @iLeaf = @iLeaf + 1 END ELSE BEGIN set @iLoop2 = @iLoop2 + 1 set @iLoop3 = 1 set @iLeaf = 1 set @iDegree = @iDegree + 1 END END ELSE BEGIN set @iLoop2 = 1 set @iLoop3 = 1 set @iDegree = 1 set @iLeaf = 1 END update TPrograms set FTreeDegree = @iDegree, FTreeLeaf = @iLeaf where FSystemID = @psSystemID and FProgramNo = @iFProgramNo SET @sTrace = 'Loop(1,2,3)=' + cast(@iLoop1 as char(2)) + ',' + cast(@iLoop2 as char(2)) + ',' + cast(@iLoop3 as char(2)) + ', Item=' + cast(@iFTreeLevel as char(2)) + ',' + cast(@iFParentNo as char(6))+ ',' + cast(@iFProgramNo as char(6)) + ',(Level, Degree, Leaf)=' + cast(@iFTreeLevel as char(2)) + ',' + cast(@iDegree as char(2)) + ',' + cast(@iLeaf as char(2)) print @sTrace FETCH NEXT FROM cus1 INTO @iFTreeLevel, @iFParentNo, @iFSequence, @iFProgramNo set @iLoop1 = @iLoop1 + 1 END CLOSE cus1 DEALLOCATE cus1 Result in messages --------------------------------- 0 -1 1 10 2 1010 2 1020 2 1030 1 20 2 2010 2 2020 2 2030 1 30 1 40 1 900 1 910 1 920 Loop(1,2,3)=1 ,1 ,1 , Item=1 ,-1 ,10 ,(Level, Degree, Leaf)=1 ,1 ,1 Loop(1,2,3)=2 ,1 ,2 , Item=1 ,-1 ,20 ,(Level, Degree, Leaf)=1 ,1 ,2 Loop(1,2,3)=3 ,1 ,3 , Item=1 ,-1 ,30 ,(Level, Degree, Leaf)=1 ,1 ,3 Loop(1,2,3)=4 ,1 ,4 , Item=1 ,-1 ,40 ,(Level, Degree, Leaf)=1 ,1 ,4 Loop(1,2,3)=5 ,1 ,5 , Item=1 ,-1 ,900 ,(Level, Degree, Leaf)=1 ,1 ,5 Loop(1,2,3)=6 ,1 ,6 , Item=1 ,-1 ,910 ,(Level, Degree, Leaf)=1 ,1 ,6 Loop(1,2,3)=7 ,1 ,7 , Item=1 ,-1 ,920 ,(Level, Degree, Leaf)=1 ,1 ,7 Loop(1,2,3)=8 ,1 ,1 , Item=2 ,10 ,1010 ,(Level, Degree, Leaf)=2 ,1 ,1 Loop(1,2,3)=9 ,1 ,2 , Item=2 ,10 ,1020 ,(Level, Degree, Leaf)=2 ,1 ,2 Loop(1,2,3)=10,1 ,3 , Item=2 ,10 ,1030 ,(Level, Degree, Leaf)=2 ,1 ,3 Loop(1,2,3)=11,2 ,1 , Item=2 ,20 ,2010 ,(Level, Degree, Leaf)=2 ,2 ,1 Loop(1,2,3)=12,2 ,2 , Item=2 ,20 ,2020 ,(Level, Degree, Leaf)=2 ,2 ,2 Loop(1,2,3)=13,2 ,3 , Item=2 ,20 ,2030 ,(Level, Degree, Leaf)=2 ,2 ,3 (1 row(s) affected)