1,用数据库字段实现之美好字段:
下图中红色箭头是属于父类:
ID 父ID 类型名称 深度 排序
深度字段是用来查询某一分类的所有子类。
跟windows文件目录管理思想一致。
类似:
在做考题的的时候,涉及到分类,虽然不是无限级的,但是,为了以后扩展用,想做成无限级,在网上找找了,一个用存储过程作的,虽然添加,编辑,移动,用的是存储过程,可是在读出来的时候只用了一条Select 语句,感觉挺爽的,下面我把存储过程列出来,我只用到了添加,编辑,没有用到移动。
1,表结构
===================================================
表结构:
表名: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)分类说明
================================================
2.添加的存储过程
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_Name,Parent_ID,Column_Path,Column_Depth,Column_Order,Column_Intro)
Values(@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
Begin
Rollback Tran
Return 0
End
2,用数据库字段实现之简单字段:
1.CREATE TABLE [dbo].[Tree] ( 2. [ID] [int] IDENTITY (1, 1) NOT NULL , 3. [PID] [int] NULL , 4. [Name] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL 5. ) ON [PRIMARY] 6. GO 7. 8. CREATE CLUSTERED INDEX [IX_Tree] ON [dbo].[Tree]([PID]) ON [PRIMARY] 9.GO 10. 11.ALTER TABLE [dbo].[Tree] WITH NOCHECK ADD 12.CONSTRAINT [PK_Tree] PRIMARY KEY NONCLUSTERED 13. ( 14. [ID] 15. ) ON [PRIMARY] , 16. CONSTRAINT [子ID不能等于父ID] CHECK ([ID] <> [PID]) 17.GO 18. 19.ALTER TABLE [dbo].[Tree] ADD 20. CONSTRAINT [FK_Tree_Tree] FOREIGN KEY 21. ( 22. [PID] 23. ) REFERENCES [dbo].[Tree] ( 24. [ID] 25. ) 26.GO 27. 28./**//****** 对象: 用户定义的函数 dbo.fGetTreeTable ******/ 29.CREATE FUNCTION dbo.fGetTreeTable 30. ( 31. @ID int= null 32. ) 33.RETURNS @Tab TABLE(ID int, PID int, Name varchar(10), Lev int) 34.AS 35. BEGIN 36. Declare @lev int 37. Set @lev=0 38. 39. While @lev=0 or @@ROWCount>0 40. Begin 41. Set @Lev=@Lev+1 42. Insert @Tab(ID, PID, Name, Lev) 43. Select ID, PID, Name, @Lev From Tree Where (@Lev=1 and ((PID=@ID) or (@ID is null and PID is null))) or (PID in (Select ID From @Tab Where Lev=@Lev-1)) 44. order by ID 45. End 46. RETURN 47. END 48. 49.GO 50. 51.--实际数据 52.Insert Tree(PID, Name) values(null, 公司) 53.Insert Tree(PID, Name) values(3, IT) 54.Insert Tree(PID, Name) values(1, Fin) 55.Insert Tree(PID, Name) values(5, XZ) 56.Insert Tree(PID, Name) values(1, HR) 57.GO