存储过程实现无限级分类(1)
在做考题的的时候,涉及到分类,虽然不是无限级的,但是,为了以后扩展用,想做成无限级,在网上找找了,一个用存储过程作的,虽然添加,编辑,移动,用的是存储过程,可是在读出来的时候只用了一条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)分类说明
================================================
表结构:
表名: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_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
GO
(
@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
GO