无限级分类设计--纯数据库实现

本文介绍了一种基于SQL Server的无限级分类系统的设计与实现,包括表结构设计、查询、添加、顺序调整、删除和修改等操作的具体实现方法。

最近在做个CMS系统,要做无限级分类。开始在网上找,希望能有现成的拿来就用,结果没搜到满意的,要吗操作复杂,要吗结构复杂,于是想自己倒腾一个,结果还算满意。

1、表结构:

 2009020908484368.jpg

 

2、查询所有分类(树型)  

ContractedBlock.gifExpandedBlockStart.gifCode
1None.gifSelect * from Category Order By OrderPath 

 

3、查询某个分类(这里假设该节点ID为 10)下属分类  

ContractedBlock.gifExpandedBlockStart.gifCode
1None.gifa、直接下属:Select * From Category Where ParentId=10 
2None.gif
3None.gif   b、本身及所有子节点:Select * From Category Where ParentPath Like '10%' Order By OrderPath 
4None.gif
5None.gif   c、所有下属子节点(不含本身):Select * From Category Where ParentPath Like '10,%' Order By OrderPath 
6None.gif

 

4:添加分类(存储过程实现):  

ContractedBlock.gifExpandedBlockStart.gifCode
 1None.gifcreate   proc Proc_InsertCategory(
 2None.gif @CategoryName varchar(50),
 3None.gif @ParentId int,
 4None.gif @Remark varchar(250)
 5None.gifas
 6None.gifbegin
 7None.gif declare @KeyId varchar(40)
 8None.gif declare @OrderId int
 9None.gif declare @CategoryId int 
10None.gif declare @Path varchar(900)
11None.gif declare @OrderPath varchar(900)
12None.gif select @KeyId=NewId()
13None.gif if @ParentId > 0
14None.gif  Select @OrderId=IsNull(Max(OrderId),0+ 1 From Category Where ParentId=@ParentId
15None.gif else
16None.gif  Select @OrderId=IsNull(Max(OrderId),0+ 1 From Category Where ParentId=CategoryId  
17None.gif 
18None.gif Insert Into Category(ChannelId,KeyId,CategoryName,ParentId,OrderId,Child,Remark)
19None.gif  Values(@ChannelId,@KeyId,@CategoryName,@ParentId,@OrderId,0,@Remark)
20None.gif Select @CategoryId=CategoryId From Category Where KeyId=@KeyId
21None.gif if @ParentId > 0
22None.gif begin
23None.gif  select @Path=ParentPath,@OrderPath=OrderPath From Category Where CategoryId=@ParentId
24None.gif  Update Category Set ParentPath=@Path + ',' + Cast(@CategoryId As Varchar(10)),OrderPath=@OrderPath + ',' + Cast(@OrderId As Varchar(10)) Where CategoryId=@CategoryId  
25None.gif  Update Category Set Child=Child + 1 Where CategoryId=@ParentId
26None.gif end
27None.gif else
28None.gif  Update Category Set ParentId=@CategoryId,Path=Cast(@CategoryId As Varchar(10)),OrderPath=Cast(@OrderId As Varchar(10)) Where CategoryId=@CategoryId
29None.gif Select @CategoryId
30None.gifend 
31None.gif
32None.gif

 

5、顺序调整:

--辅助过程

ContractedBlock.gifExpandedBlockStart.gifCode
 1None.gifcreate   Proc Proc_ResetCategoryOrder(
 2None.gif @CategoryId int,
 3None.gif @OrderId int
 4None.gifas
 5None.gifbegin
 6None.gif Update Category
 7None.gif  Set OrderId=@OrderId,OrderPath=(Select OrderPath From Category Where CategoryId=(Select ParentId From Category Where CategoryId=@CategoryId)) + ',' + cast(@OrderId As varchar(10))
 8None.gif Where CategoryId=@CategoryId 
 9None.gif
10None.gif Update Category
11None.gif  Set OrderPath=(Select OrderPath From Category Where CategoryId=@CategoryId+ ',' + cast(OrderId As varchar(10))
12None.gif Where ParentPath like (Select ParentPath From Category Where CategoryId=@CategoryId+ ',%'
13None.gifend 
14None.gif

 

--修改分类序号

ContractedBlock.gifExpandedBlockStart.gifCode
 1None.gifcreate   Proc Proc_ChangeCategoryOrder(
 2None.gif @CategoryId int,
 3None.gif @NewOrderId int
 4None.gifas
 5None.gifbegin
 6None.gif exec Proc_ResetCategoryOrder @CategoryId,@NewOrderId
 7None.gif declare @OldOrderId int
 8None.gif declare @ParentId int
 9None.gif declare @cid int
10None.gif declare @oid int
11None.gif Select @OldOrderId=OrderId,@ParentId=ParentId From Category Where CategoryId=@CategoryId
12None.gif if @OldOrderId=@NewOrderId
13None.gif  return
14None.gif declare @Relation_Category cursor 
15None.gif
16None.gif if @ParentId = @CategoryId
17None.gif begin
18None.gif  if @OldOrderId>@NewOrderId
19None.gif   set @Relation_Category = cursor for select CategoryId,OrderId+1 From Category Where CategoryId=ParentId And OrderId<@OldOrderId And OrderId>@NewOrderId
20None.gif  else
21None.gif   set @Relation_Category = cursor for select CategoryId,OrderId-1 From Category Where CategoryId=ParentId And OrderId>@OldOrderId And OrderId<@NewOrderId
22None.gif end
23None.gif else
24None.gif begin
25None.gif  if @OldOrderId>@NewOrderId
26None.gif   set @Relation_Category = cursor for select CategoryId,OrderId+1 From Category Where CategoryId=@ParentId And OrderId<@OldOrderId And OrderId>@NewOrderId
27None.gif  else
28None.gif   set @Relation_Category = cursor for select CategoryId,OrderId-1 From Category Where CategoryId=@ParentId And OrderId>@OldOrderId And OrderId<@NewOrderId
29None.gif end
30None.gif open @Relation_Category
31None.gif fetch next from @Relation_Category into @cid,@oid
32None.gif while @@fetch_status=0
33None.gif begin
34None.gif  exec Proc_ResetCategoryOrder @cid,@oid 
35None.gif  fetch next from @Relation_Category into @cid,@oid
36None.gif end
37None.gif close @Relation_Category
38None.gif DEALLOCATE @Relation_Category
39None.gifend 
40None.gif

 

6、删除分类:

ContractedBlock.gifExpandedBlockStart.gifCode
1None.gif Delete From Category Where ParentPath Like (Select ParentPath From Category Where CategoryId=@CategoryId+ '%' 

 

7、修改:

就只是修改名称和备注,直接更新就可以了

开始的时候觉得好像很难,做出来了才发现,很简单的嘛

转载于:https://www.cnblogs.com/sandou/archive/2009/02/09/1386583.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值