或多或少都做過樹狀目錄、產品分類之類的二級或三級菜單,如果遇到更多級的分類,就一般使用遞歸了。在程序中使用遞歸或多或少會增加一些性能上的開銷。
之前我用ASP.Net在程序中實現過非遞歸的無限級分類目錄,但考慮到移植性不強,就改成了存儲過程,發出來大家共同研究一下,到目前為止,測試過程中還沒發現問題,另外,代碼方面沒經過什麼優化。
通常情況下,我們更多的操作是讀取目錄,所以,在下面的實現中,讀取我們只需要一Select語句就夠了,不使用遞歸,理論上無限級~!
======================================================================
表結構:
表名:Tb_Column
表結構(所有字段非空):
Column_ID int 主鍵(注:非標識)
Column_Name nvarchar(50)分類名稱
Parent_ID int 父分類ID(默認值0)
Column_Path nvarchar(1000) 分類路徑
Column_Depth int分類深度(默認值0)
Column_Order int排序(默認值0)
Column_Intro nvarchar(1000)分類說明
=============================================================
存儲過程一:新建分類
CREATE PROCEDURE sp_Column_Insert
(
@Parent_ID int,
@Column_Name nvarchar(50),
@Column_Intro nvarchar(1000)
)
AS
Declare @Err As int
Set @Err=0
Begin Tran
--通過現有記錄獲取欄目ID
Declare @Column_ID As int
Declare @Column_Depth As int
Select @Column_ID = Max(Column_ID) From Tb_Column
IF @Column_ID Is Not Null
Set @Column_ID = @Column_ID+1
Else
Set @Column_ID = 1
--判斷是否是頂級欄目,設置其Column_Path和Column_Order
Declare @Column_Path As nvarchar(1000)
Declare @Column_Order As int
IF @Parent_ID = 0
Begin
Set @Column_Path =Ltrim(Str(@Column_ID))
Select @Column_Order = Max(Column_Order) From Tb_Column
IF @Column_Order Is Not Null
Set @Column_Order = @Column_Order + 1
Else --如果沒有查詢到記錄,說明這是第一條記錄
Set @Column_Order = 1
--深度
Set @Column_Depth = 1
End
Else
Begin
--獲取父節點的路徑和深度
Select @Column_Path = Column_Path ,@Column_Depth = Column_Depth From Tb_Column Where
Column_ID=@Parent_ID
IF @Column_Path Is Null
Begin
Set @Err = 1
Goto theEnd
End
--獲取同父節點下的最大序號
Select @Column_Order = Max(Column_Order) From Tb_PicColumn Where Column_Path like
''+@Column_Path+'|%' Or Column_ID = @Parent_ID
IF @Column_Order Is Not Null --如果序號存在,那麼將該序號後的所有序號都加1
Begin
--更新當前要插入節點後所有節點的序號
Update Tb_Column Set Column_Order = Column_Order +1 Where Column_Order
>@Column_Order
--同父節點下的最大序號加上1,構成自己的序號
Set @Column_Order = @Column_Order + 1
End
Else
Begin
Set @Err=1
Goto theEnd
End
--父節點的路徑加上自己的ID號,構成自己的路徑
Set @Column_Path = @Column_Path + '|' + Ltrim(Str(@Column_ID))
--深度
Set @Column_Depth = @Column_Depth+1
End
Insert Into Tb_Column(Column_ID,Column_Name,Parent_ID,Column_Path,Column_Depth,Column_Order,Column_Intro)
Values(@Column_ID,@Column_Name,@Parent_ID,@Column_Path,@Column_Depth,@Column_Order,@Column_Intro)
IF @@Error<>0
Begin
Set @Err=1
Goto theEnd
End
--更新當前記錄之後的記錄的ORDER
--Update Tb_Column Set Column_Order = Column_Order+1 Where Column_Order > @Column_Order
theEnd:
IF @Err=0
Begin
Commit Tran
Return @Column_ID
End
Else
Be
gin
Rollback Tran
Return 0
End
GO
=============================================================================
存儲過程二:刪除分類
CREATE PROCEDURE sp_Column_Delete
(
@Column_ID int
)
AS
Declare @Err As int
Set @Err = 0
Begin Tran
--首先查詢該節點下是否有子節點
Select Column_ID From Tb_Column Where Parent_ID = @Column_ID
IF @@RowCount<>0
Begin
Set @Err = 1
Goto theEnd
End
--獲取該節點的Column_Order,為了刪除後整理其他記錄的順序
Declare @Column_Order As int
Select @Column_Order = Column_Order From Tb_Column Where Column_ID = @Column_ID
IF @Column_Order Is NUll
Begin
Set @Err =2
Goto theEnd
End
--更新其他記錄的Column_Order
Update Tb_Column Set Column_Order = Column_Order -1 Where Column_Order >@Column_Order
IF @@Error<>0
Begin
Set @Err =3
Goto theEnd
End
--刪除操作
Delete From Tb_Column Where Column_ID=@Column_ID
IF @@Error<>0
Begin
Set @Err =4
Goto theEnd
End
--更新其他記錄的Column_ID
--Update Tb_Column Set Column_ID= Column_ID - 1 Where Column_ID >@Column_ID
--IF @@Error<>0
-- Begin
-- Set @Err =5
-- Goto theEnd
-- End
theEnd:
IF @Err = 0
Begin
Commit Tran
Return 0 --刪除成功
End
Else
Begin
IF @Err=1
Begin
Rollback Tran
Return 1 --有子節點
End
Else
Begin
Rollback Tran
Return 2--未知錯誤
End
End
GO
==================================================================
存儲過程三:編輯分類
CREATE PROCEDURE sp_Column_Update
(
@Column_ID int,
@Parent_ID int,
@Column_Name nvarchar(50),
@Column_Intro nvarchar(1000)
)
AS
Declare @Err As int
Set @Err=0
Begin Tran
--獲取修改前的:Parent_ID,Column_Depth,Column_Order
Declare @oParent_ID As int
Declare @oColumn_Depth As int
Declare @oColumn_Order As int
Declare @oColumn_Path As nvarchar(1000)
Select @oParent_ID = Parent_ID, @oColumn_Depth = Column_Depth,@oColumn_Order = Column_Order, @oColumn_Path = Column_Path From Tb_Column Where Column_ID = @Column_ID
IF @oParent_ID Is Null
Begin
Set @Err = 1
Goto theEnd
End
--如果父ID沒有改變,則直接修改欄目名和欄目簡介
IF @oParent_ID = @Parent_ID
Begin
Update Tb_Column Set Column_Name = @Column_Name,Column_Intro = @Column_Intro Where Column_ID = @Column_ID
IF @@Error <> 0
Set @Err = 2
Goto theEnd
End
Declare @nColumn_Path As nvarchar(1000)
Declare @nColumn_Depth As int
Declare @nColumn_Order As int
--獲取當前節點作為父節點所包含的節點數[包括自身] 注:如果返回 “1” 說明是單節點
Declare @theCount As int
Select @theCount = Count(Column_ID) From Tb_Column Where Column_ID=@Column_ID Or Column_Path like ''+@oColumn_Path+'|%'
IF @theCount Is Null
Begin
Set @Err = 3
Goto theEnd
End
IF @Parent_ID=0 --如果是設置為頂級節點,將節點設置為最後一個頂級節點
Begin
--Print '設置為頂級欄目'
Set @nColumn_Path = Ltrim(Str(@Column_ID))
Set @nColumn_Depth =1
Select @nColumn_Order = Max(Column_Order) From Tb_Column
IF @nColumn_Order Is NULL
Begin
Set @Err = 4
Goto theEnd
End
Set @nColumn_Order = @nColumn_Order - @theCount + 1
--更新三部分 1 節點本身 2 所有子節點 2 本樹更改之前的後面記錄的順序
--Print '更新本欄目之前位置後面的所有欄目[不包括本欄目下的子欄目]的:Column_Order'
Update Tb_Column Set Column_Order = Column_Order-@theCount Where (Column_Order >@oColumn_Order) And (Column_Path Not like ''+@oColumn_Path+'|%')
IF @@Error <> 0
Begin
Set @Err = 7
Goto theEnd
End
--Print '更新本欄目的:Parent_ID,Column_Path,Column_Depth,Column_Order,Column_Name,Column_Intro'
Print 'Order : '+Ltrim(Str(@nColumn_Order))
Update Tb_Column Set Parent_ID=@Parent_ID,Column_Path = @nColumn_Path,Column_Depth = @nColumn_Depth,Column_Order = @nColumn_Order, Column_Name = @Column_Name,Column_Intro = @Column_Intro Where Column_ID = @Column_ID
IF @@Error <> 0
Begin
Set @Err = 5
Goto theEnd
End
--Print '更新本欄目下的所有子欄目的:Column_Path,Column_Depth,Column_Order'
Update Tb_Column Set Column_Path = Replace(Column_Path,@oColumn_Path,@nColumn_Path),Column_Depth = Column_Depth + (@nColumn_Depth-@oColumn_Depth),Column_Order = Column_Order+( @nColumn_Order-@oColumn_Order) Where Column_Path like ''+@oColumn_Path+'|%'
IF @@Error <> 0
Begin
Set @Err = 6
Goto theEnd
End
End
Else
Begin
--獲取未來父節點的相關信息,並設置本節點的相關值
Select @nColumn_Depth = Column_Depth,@nColumn_Path = Column_Path From Tb_Column Where Column_ID = @Pare
nt_ID
IF @nColumn_Depth Is NULL Or @nColumn_Path Is Null
Begin
Set @Err = 8
Goto theEnd
End
Set @nColumn_Depth = @nColumn_Depth +1
Select @nColumn_Order =Max(Column_Order) From Tb_Column Where Column_ID = @Parent_ID Or Column_Path like ''+@nColumn_Path+'|%'
IF @nColumn_Order Is NULL
Begin
Set @Err = 9
Goto theEnd
End
Set @nColumn_Path = @nColumn_Path +'|'+ Ltrim(Str(@Column_ID))
IF @nColumn_Order = @oColumn_Order+1 --如果新的父節點是原來位置上端最近一個兄弟,則所有節點的順序都不改變
Begin
Update Tb_Column Set Parent_ID=@Parent_ID,Column_Path = @nColumn_Path,Column_Depth = @nColumn_Depth, Column_Name = @Column_Name,Column_Intro = @Column_Intro Where Column_ID = @Column_ID
IF @@Error <> 0
Begin
Set @Err = 10
Goto theEnd
End
End
Set @nColumn_Order = @nColumn_Order + 1
--更新三部分 1 本樹更改之前的後面(或前面)記錄的順序 1 節點本身 3 所有子節點
--分為向上移或象下移
--Print '更新本欄目之前位置後面的所有欄目[或者本欄目之後位置] [不包括本欄目下的子欄目]的:Column_Order'
IF @nColumn_Order < @oColumn_Order
Begin
Update Tb_Column Set Column_Order = Column_Order+@theCount Where Column_Order<@oColumn_Order And Column_Order >=@nColumn_Order And (Column_Path Not like ''+@oColumn_Path+'|%') And Column_ID<>@Column_ID
IF @@Error <> 0
Begin
Set @Err = 12
Goto theEnd
End
End
Else
Begin
Update Tb_Column Set Column_Order = Column_Order-@theCount Where Column_Order >@oColumn_Order And Column_Order<@nColumn_Order And (Column_Path Not like ''+@oColumn_Path+'|%') And Column_ID<>@Column_ID
IF @@Error <> 0
Begin
Set @Err = 13
Goto theEnd
End
End
--Print '更新本欄目的:Parent_ID,Column_Path,Column_Depth,Column_Order,Column_Name,Column_Intro'
Print 'Order : '+Ltrim(Str(@nColumn_Order))
IF @nColumn_Order > @oColumn_Order
Set @nColumn_Order = @nColumn_Order - @theCount
Update Tb_Column Set Parent_ID=@Parent_ID,Column_Path = @nColumn_Path,Column_Depth = @nColumn_Depth,Column_Order = @nColumn_Order, Column_Name = @Column_Name,Column_Intro = @Column_Intro Where Column_ID = @Column_ID
IF @@Error <> 0
&nb
sp; Begin
Set @Err = 10
Goto theEnd
End
--Print '更新本欄目下的所有子欄目的:Column_Paht,Column_Depth,Column_Order'
Update Tb_Column Set Column_Path = Replace(Column_Path,@oColumn_Path,@nColumn_Path),Column_Depth = Column_Depth + (@nColumn_Depth-@oColumn_Depth),Column_Order = Column_Order+(@nColumn_Order-@oColumn_Order) Where Column_Path like ''+@oColumn_Path+'|%'
IF @@Error <> 0
Begin
Set @Err = 11
Goto theEnd
End
End
theEnd:
IF @Err<>0 --如果有錯誤則返回錯誤號
Begin
Rollback Tran
Return @Err
End
Else --如果沒有錯誤就返回0
Begin
Commit Tran
Return 0
End
GO
===========================================================================
存儲過程四:顯示分類(只是一條select語句)
分類列表:
CREATE PROCEDURE sp_Column_List
AS
SELECT Column_ID, Column_Name, Parent_ID, Column_Path, Column_Depth,
Column_Order, Column_Intro
FROM Tb_Column
ORDER BY Column_Order
GO
=======================================
這裡有一個在ASP.Net下使用的例子,放在朋友一個論壇上:
http://www.mzline.com/bbs/dispbbs.ASP?boardID=67&ID=5044&replyID=25788&star=1&skin=0#25788