sql 函数实现三种父子递归
2012-12-09 22:45
316 查看
在实际运用中经常会创建这样的结构表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……递归(树形结构数据)查询
- java实现二叉树的创建及三种递归遍历
- C语言-函数实现模块化设计-函数的递归调用
- 工作中常用的php递归实现无限极分类的2种函数
- 二叉树的三种遍历方式的循环和递归的实现方式
- sql 自定义函数 ,where后拼接条件跳转语句,实现可变参数查询
- SQL2005 With as 公用表表达式,实现递归取数
- PHP+Mysql实现多关键字与多字段生成SQL语句的函数
- 使用over函数实现递归汇总
- SQL 2005 CLR 函数实现简繁转换功能
- php实现无限级树型菜单(函数递归做法)
- select 函数实现 三种拓扑结构 n个客户端的异步通信 (完全图+线性链表+无环图)
- 【C语言】编写一个函数实现n^k,使用递归实现
- C和指针之函数递归实现把amount表示的值转换为单词形式written_amount(unsigned int amount,char *buffer)
- 关于Oracle中split函数的实现以及函数递归的举例。