* 标题:查询各节点的父路径函数 作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 时间:2008-05-12 地点:广东深圳 *//* 原始数据及要求结果如下: --食品 --水果 --香蕉 --苹果 --蔬菜 --青菜 id pid name ----------- ----------- -------------------- 1 0 食品 2 1 水果 3 1 蔬菜 4 2 香蕉 5 2 苹果 6 3 青菜要求得到各节点的父路径即如下结果: id pid name 路径 --- --- ----- --------------- 1 0 食品 食品 2 1 水果 食品,水果 3 1 蔬菜 食品,蔬菜 4 2 香蕉 食品,水果,香蕉 5 2 苹果 食品,水果,苹果 6 3 青菜 食品,蔬菜,青菜 */create table tb (id int , pid int , name nvarchar(20)) insert into tb values(1 , 0 , '食品') insert into tb values(2 , 1 , '水果') insert into tb values(3 , 1 , '蔬菜') insert into tb values(4 , 2 , '香蕉') insert into tb values(5 , 2 , '苹果') insert into tb values(6 , 3 , '青菜') go--查询各节点的父路径函数 create function f_pid(@id int) returns varchar(100) as begin declare @re_str as varchar(100) set @re_str = '' select @re_str = name from tb where id = @id while exists (select 1 from tb where id = @id and pid <> 0) begin select @id = b.id , @re_str = b.name + ',' + @re_str from tb a , tb b where a.id = @id and a.pid = b.id end return @re_str end goselect * , dbo.f_pid(id) 路径 from tb order by iddrop table tb drop function f_pidSQL code /* 标题:查询所有节点及其所有子节点的函数 作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 时间:2009-04-12 地点:广东深圳 */--生成测试数据 create table tb(id varchar(10),pid varchar(10)) insert into tb select 'a', null insert into tb select 'b', 'a' insert into tb select 'c', 'a' insert into tb select 'd', 'b' insert into tb select 'e', 'b' insert into tb select 'f', 'c' insert into tb select 'g', 'c' go --创建用户定义函数 create function f_getchild(@id varchar(10)) returns varchar(8000) as begin declare @i int , @ret varchar(8000) declare @t table(id varchar(10) , pid varchar(10) , level int) set @i = 1 insert into @t select id , pid , @i from tb where id = @id while @@rowcount <> 0 begin set @i = @i + 1 insert into @t select a.id , a.pid , @i from tb a , @t b where a.pid = b.id and b.level = @i - 1 end select @ret = isnull(@ret , '') + id + ',' from @t return left(@ret , len(@ret) - 1) end go --执行查询 select id , children = isnull(dbo.f_getchild(id) , '') from tb group by id go --输出结果 /* id children ---------- ------------- a a,b,c,d,e,f,g b b,d,e c c,f,g d d e e f f g g(所影响的行数为 7 行)*/ --删除测试数据 drop function f_getchild drop table tb
2005的 with wang as (select *,path=cast(name as varchar(100)) from tb where parentid=0 union all select tb.*,name=cast(wang.path+tb.name as varchar(100)) from tb join wang on tb.parentid=wang.id)select * from wang
/* 标题:查询指定节点及其所有子节点的函数 作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 时间:2008-05-12 地点:广东深圳 */create table tb(id varchar(3) , pid varchar(3) , name varchar(10)) insert into tb values('001' , null , '广东省') insert into tb values('002' , '001' , '广州市') insert into tb values('003' , '001' , '深圳市') insert into tb values('004' , '002' , '天河区') insert into tb values('005' , '003' , '罗湖区') insert into tb values('006' , '003' , '福田区') insert into tb values('007' , '003' , '宝安区') insert into tb values('008' , '007' , '西乡镇') insert into tb values('009' , '007' , '龙华镇') insert into tb values('010' , '007' , '松岗镇') go--查询指定节点及其所有子节点的函数 create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int) as begin declare @level int set @level = 1 insert into @t_level select @id , @level while @@ROWCOUNT > 0 begin set @level = @level + 1 insert into @t_level select a.id , @level from tb a , @t_Level b where a.pid = b.id and b.level = @level - 1 end return end go--调用函数查询001(广东省)及其所有子节点 select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id /* id pid name ---- ---- ---------- 001 NULL 广东省 002 001 广州市 003 001 深圳市 004 002 天河区 005 003 罗湖区 006 003 福田区 007 003 宝安区 008 007 西乡镇 009 007 龙华镇 010 007 松岗镇(所影响的行数为 10 行) */--调用函数查询002(广州市)及其所有子节点 select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id /* id pid name ---- ---- ---------- 002 001 广州市 004 002 天河区(所影响的行数为 2 行) */--调用函数查询003(深圳市)及其所有子节点 select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id /* id pid name ---- ---- ---------- 003 001 深圳市 005 003 罗湖区 006 003 福田区 007 003 宝安区 008 007 西乡镇 009 007 龙华镇 010 007 松岗镇(所影响的行数为 7 行) */drop table tb drop function f_cid
---修改下影子哥的 ---------------------------------------------------------------- -- Author :fredrickhu(小F,向高手学习) -- Date :2009-12-11 19:25:33 -- Version: -- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) -- Nov 24 2008 13:01:59 -- Copyright (c) 1988-2005 Microsoft Corporation -- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3) -- ---------------------------------------------------------------- --> 测试数据:[tb] if object_id('[tb]') is not null drop table [tb] go create table [tb]([ID] int,[Name] varchar(6),[ParentID] int) insert [tb] select 1,'广东',0 union all select 2,'深圳',0 union all select 3,'天河区',1 union all select 4,'白云区',1 union all select 5,'宝安区',2 --------------开始查询-------------------------- ;with f as ( select *,cast([name] as varchar(100)) as [path] from tb where parentid=0 union all select a.*, cast(b.name+a.name as varchar(100)) as [name] from tb a , f b where a.parentid=b.id ) select * from f----------------结果---------------------------- /*ID Name ParentID path ----------- ------ ----------- ---------------------------------------------------------------------------------------------------- 1 广东 0 广东 2 深圳 0 深圳 5 宝安区 2 深圳宝安区 3 天河区 1 广东天河区 4 白云区 1 广东白云区(5 行受影响) */
标题:查询各节点的父路径函数
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*//*
原始数据及要求结果如下:
--食品
--水果
--香蕉
--苹果
--蔬菜
--青菜
id pid name
----------- ----------- --------------------
1 0 食品
2 1 水果
3 1 蔬菜
4 2 香蕉
5 2 苹果
6 3 青菜要求得到各节点的父路径即如下结果:
id pid name 路径
--- --- ----- ---------------
1 0 食品 食品
2 1 水果 食品,水果
3 1 蔬菜 食品,蔬菜
4 2 香蕉 食品,水果,香蕉
5 2 苹果 食品,水果,苹果
6 3 青菜 食品,蔬菜,青菜
*/create table tb (id int , pid int , name nvarchar(20))
insert into tb values(1 , 0 , '食品')
insert into tb values(2 , 1 , '水果')
insert into tb values(3 , 1 , '蔬菜')
insert into tb values(4 , 2 , '香蕉')
insert into tb values(5 , 2 , '苹果')
insert into tb values(6 , 3 , '青菜')
go--查询各节点的父路径函数
create function f_pid(@id int) returns varchar(100)
as
begin
declare @re_str as varchar(100)
set @re_str = ''
select @re_str = name from tb where id = @id
while exists (select 1 from tb where id = @id and pid <> 0)
begin
select @id = b.id , @re_str = b.name + ',' + @re_str from tb a , tb b where a.id = @id and a.pid = b.id
end
return @re_str
end
goselect * , dbo.f_pid(id) 路径 from tb order by iddrop table tb
drop function f_pidSQL code
/*
标题:查询所有节点及其所有子节点的函数
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2009-04-12
地点:广东深圳
*/--生成测试数据
create table tb(id varchar(10),pid varchar(10))
insert into tb select 'a', null
insert into tb select 'b', 'a'
insert into tb select 'c', 'a'
insert into tb select 'd', 'b'
insert into tb select 'e', 'b'
insert into tb select 'f', 'c'
insert into tb select 'g', 'c'
go --创建用户定义函数
create function f_getchild(@id varchar(10)) returns varchar(8000)
as
begin
declare @i int , @ret varchar(8000)
declare @t table(id varchar(10) , pid varchar(10) , level int)
set @i = 1
insert into @t select id , pid , @i from tb where id = @id
while @@rowcount <> 0
begin
set @i = @i + 1
insert into @t select a.id , a.pid , @i from tb a , @t b where a.pid = b.id and b.level = @i - 1
end
select @ret = isnull(@ret , '') + id + ',' from @t
return left(@ret , len(@ret) - 1)
end
go --执行查询
select id , children = isnull(dbo.f_getchild(id) , '') from tb group by id
go --输出结果
/*
id children
---------- -------------
a a,b,c,d,e,f,g
b b,d,e
c c,f,g
d d
e e
f f
g g(所影响的行数为 7 行)*/ --删除测试数据
drop function f_getchild
drop table tb
2 深圳 0
3 天河区 1
4 白云区 1
5 宝安区 2
查询出的结果如我上面说的!!!谢谢了!!!
with
wang as (select *,path=cast(name as varchar(100)) from tb where parentid=0
union all
select tb.*,name=cast(wang.path+tb.name as varchar(100)) from tb join wang
on tb.parentid=wang.id)select * from wang
标题:查询指定节点及其所有子节点的函数
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go--查询指定节点及其所有子节点的函数
create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.id , @level
from tb a , @t_Level b
where a.pid = b.id and b.level = @level - 1
end
return
end
go--调用函数查询001(广东省)及其所有子节点
select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市
003 001 深圳市
004 002 天河区
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇(所影响的行数为 10 行)
*/--调用函数查询002(广州市)及其所有子节点
select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
002 001 广州市
004 002 天河区(所影响的行数为 2 行)
*/--调用函数查询003(深圳市)及其所有子节点
select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
003 001 深圳市
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇(所影响的行数为 7 行)
*/drop table tb
drop function f_cid
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-12-11 19:25:33
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[Name] varchar(6),[ParentID] int)
insert [tb]
select 1,'广东',0 union all
select 2,'深圳',0 union all
select 3,'天河区',1 union all
select 4,'白云区',1 union all
select 5,'宝安区',2
--------------开始查询--------------------------
;with f as
(
select *,cast([name] as varchar(100)) as [path] from tb where parentid=0
union all
select
a.*,
cast(b.name+a.name as varchar(100)) as [name]
from
tb a , f b
where
a.parentid=b.id
)
select * from f----------------结果----------------------------
/*ID Name ParentID path
----------- ------ ----------- ----------------------------------------------------------------------------------------------------
1 广东 0 广东
2 深圳 0 深圳
5 宝安区 2 深圳宝安区
3 天河区 1 广东天河区
4 白云区 1 广东白云区(5 行受影响)
*/