sql 函数实现三种父子递归
2012-04-24 17:21
288 查看
在实际运用中经常会创建这样的结构表Category(Id, ParentId, Name),特别是用于树形结构时(菜单树,权限树..),这种表设计自然而然地会用到递归,若是在程序中进行递归(虽然在程序中递归真的更方便一些),无论是通过ADO.NET简单sql查找还是ORM属性关联都会执行多次sql语句,难免会造成一些性能上的损耗,所以干脆使用sql的函数来解决这个问题,用函数返回我们最终需要的结果。
针对这类需求,这里我列出三种常用的递归:
以一个节点为基点,列出所有子节点直到无子 (找下级) 。这有点儿像点兵点将,主帅只有一个,下面是左将、右将,左将下面又有千夫长、百夫长,点兵时主帅下令集合,下面的将军只管各自的队伍。
以一个节点为基点,列出所有父节点直到祖先(找上级) 。
面包屑导航数据(单条数据)
下面我以一幅图列出这三种形式(实线表现的是我们最终想要的数据,第三幅图中只有一条数据):
OK,现在让我们来实现这几个需求,step by step。
1. 数据准备
根据上面的图中的数据创建表结构和测试数据
2. 正向递归实现
执行上面的函数得到如下图的结果:
-----------------------------------------------------------------------------------------------------------------------------
当然自sql 2005后微软提供了CTE(公用表表达式)也可以用于递归查询,请参阅使用公用表达式的递归查询。
上面的递归用CTE的sql代码如下:
3. 逆向递归实现
执行这个函数得到的结果如下:
4. 面包屑实现
调用这个函数的结果如下:
本文sql源代码下载:http://files.cnblogs.com/keepfool/region_recursive.zip
针对这类需求,这里我列出三种常用的递归:
以一个节点为基点,列出所有子节点直到无子 (找下级) 。这有点儿像点兵点将,主帅只有一个,下面是左将、右将,左将下面又有千夫长、百夫长,点兵时主帅下令集合,下面的将军只管各自的队伍。
以一个节点为基点,列出所有父节点直到祖先(找上级) 。
面包屑导航数据(单条数据)
下面我以一幅图列出这三种形式(实线表现的是我们最终想要的数据,第三幅图中只有一条数据):
OK,现在让我们来实现这几个需求,step by step。
1. 数据准备
根据上面的图中的数据创建表结构和测试数据
create table Region ( Id int identity primary key, Name nvarchar(20), ParentId int ) go insert into Region(Name,ParentId) values('广东',NULL) insert into Region(Name,ParentId) values('深圳',1) insert into Region(Name,ParentId) values('惠州',1) insert into Region(Name,ParentId) values('罗湖区',2) insert into Region(Name,ParentId) values('福田区',2) insert into Region(Name,ParentId) values('龙岗区',2) insert into Region(Name,ParentId) values('惠阳区',3) insert into Region(Name,ParentId) values('龙门县',3) insert into Region(Name,ParentId) values('华强北',5) insert into Region(Name,ParentId) values('体育馆',5) select * from Region
2. 正向递归实现
/* * summary:递归获取所有子节点 */ alter function GetRecursiveChildren ( @Id int ) returns @t table(Id int,ParentId int,Name nvarchar(20), [Level] int) begin declare @i int set @i = 1 --根节点,Level = 0 insert into @t select @Id,@id,(select Name from Region where Id = @id),0 --直属子节点,Level = 1 insert into @t select Id,ParentId,Name,@i from Region where ParentId = @Id --如果没有新的值插入,循环结束 while @@rowcount<>0 begin set @i = @i + 1; insert into @t select a.Id,a.ParentId,a.Name,@i from Region a, @t b where a.ParentId = b.Id and b.Level = @i - 1 end return end go --调用函数 select * from GetRecursiveChildren(2)
执行上面的函数得到如下图的结果:
-----------------------------------------------------------------------------------------------------------------------------
当然自sql 2005后微软提供了CTE(公用表表达式)也可以用于递归查询,请参阅使用公用表达式的递归查询。
上面的递归用CTE的sql代码如下:
declare @id int set @id = 2 ;with t as--如果CTE前面有语句,需要用分号隔断 ( select Id, ParentId, Name from Region where Id = @id union all select r1.Id,r1.ParentId,r1.Name from Region r1 join t as r2 on r1.ParentId = r2.Id ) select * from t order by Id
3. 逆向递归实现
create function GetRecursiveParent ( @Id int ) returns @t table(Id int,ParentId int,Name nvarchar(20), [Level] int) as begin declare @i int set @i = 1 --插入末节点,Level = 0 insert into @t select @Id,@id,(select Name from Region where Id = @id),0 --插入末节点的父节点,Level = 1 insert into @t select Id,ParentId,Name,@i from Region where Id = (select ParentId from Region where Id = @Id) --如果没有新的值插入,循环结束 while @@rowcount<>0 begin set @i = @i + 1; insert into @t select a.Id,a.ParentId,a.Name,@i from Region a, @t b where a.Id = b.ParentId and b.Level = @i - 1 end return end go --调用函数 select * from GetRecursiveParent(10) go
执行这个函数得到的结果如下:
4. 面包屑实现
create function GetLevel ( @Id int ) returns @level table(IdLevel varchar(100),NameLevel nvarchar(200)) as begin declare @IdLevel varchar(100),@NameLevel nvarchar(200),@Name nvarchar(50) select @IdLevel = cast(@Id as varchar(10)) select @NameLevel = (select Name from Region where Id = @Id) while(exists(select Id,ParentId from Region where Id = (select ParentId from Region where Id = @Id))) begin select @Id = Id,@Name = Name from Region where Id = (select ParentId from Region where Id = @Id) select @IdLevel = cast(@Id as varchar(10)) + '>' + @IdLevel select @NameLevel = @Name + '>' + @NameLevel end insert into @level select @IdLevel,@NameLevel return end go --调用函数 select * from GetLevel(10) go
调用这个函数的结果如下:
本文sql源代码下载:http://files.cnblogs.com/keepfool/region_recursive.zip
相关文章推荐
- sql 函数实现三种父子递归
- sql 函数实现三种父子递归
- sql 函数实现三种父子递归
- sql 函数实现三种父子递归
- MySql的sql语句中添加存储过程或者存储函数来实现Oracle中的start with ……connect by prior……递归(树形结构数据)查询
- 链表插入元素的三种递归实现 -- 简单递归,数据累加器,函数累加器
- 递归实现二叉树的三种遍历
- SQL实现类似C#的Split()函数的功能
- 3.1题目: 利用递归方法实现一个函数,该函数能够实现n的阶乘,即 n! = n*(n-1)*…*3*2*1;
- PHP实现递归的三种方法
- 递归,迭代,堆栈三种方式实现单链表反转(C++)
- 实现CADOConn类的打开记录集函数:_RecordsetPtr& OpenRecordset(CString sql)
- SQL中实现截取字符串的函数
- 【C语言】编写一个函数实现n^k,使用递归实现
- 分别使用函数及游标实现SQL多行转一列
- 用递归实现字符串分割(Split)[SQL 2005]
- 在Access中实现T-SQL中的ISNULL函数功能
- 二叉树三种非递归遍历实现
- C语言链表基本函数的递归实现
- 编写一个函数reverse_string(char * string)(递归实现)