;with t as ( select userid,parentid, cast(ltrim(parentid) as varchar(max)) relationship from table where parentid=0 union all select t1.userid,t1.parentid, cast(t.relationship+','+ltrim(userid) as varchar(max)) from table t1, t where t.userid=t1.parentid ) select userid,relationship from t;
/* 标题:SQL SERVER 2000中查询指定节点及其所有子节点的函数(字符串形式显示) 作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 时间:2010-02-02 地点:新疆乌鲁木齐 */--生成测试数据 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(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_cid(id) , '') from tb group by iddrop table tb drop function f_cid/* id children ---- --------------------------------------- 001 001,002,003,004,005,006,007,008,009,010 002 002,004 003 003,005,006,007,008,009,010 004 004 005 005 006 006 007 007,008,009,010 008 008 009 009 010 010(所影响的行数为 10 行) */
/* 标题:SQL SERVER 2005中查询指定节点及其所有子节点的方法(字符串形式显示) 作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 时间:2010-02-02 地点:新疆乌鲁木齐 */create table tb(id varchar(3) , pid varchar(3) , name nvarchar(10)) insert into tb values('001' , null , N'广东省') insert into tb values('002' , '001' , N'广州市') insert into tb values('003' , '001' , N'深圳市') insert into tb values('004' , '002' , N'天河区') insert into tb values('005' , '003' , N'罗湖区') insert into tb values('006' , '003' , N'福田区') insert into tb values('007' , '003' , N'宝安区') insert into tb values('008' , '007' , N'西乡镇') insert into tb values('009' , '007' , N'龙华镇') insert into tb values('010' , '007' , N'松岗镇') go;with t as ( select id , cid = id from tb union all select t.id , cid = tb.id from t join tb on tb.pid = t.cid ) select id , cid = STUFF((SELECT ',' + rtrim(cid) FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , '') from tb group by id order by id /* id cid ---- --------------------------------------- 001 001,002,003,005,006,007,008,009,010,004 002 002,004 003 003,005,006,007,008,009,010 004 004 005 005 006 006 007 007,008,009,010 008 008 009 009 010 010(10 行受影响) */;with t as ( select id , name , cid = id , path = cast(name as nvarchar(100)) from tb union all select t.id , t.name , cid = tb.id , path = cast(tb.name as nvarchar(100)) from t join tb on tb.pid = t.cid ) select id , name , cid = STUFF((SELECT ',' + rtrim(cid) FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , ''), path = STUFF((SELECT ',' + path FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , '') from tb group by id , name order by id /* id name cid path ---- ---------- ------------------------------------------- --------------------------------------------------------------------- 001 广东省 001,002,003,005,006,007,008,009,010,004 广东省,广州市,深圳市,罗湖区,福田区,宝安区,西乡镇,龙华镇,松岗镇,天河区 002 广州市 002,004 广州市,天河区 003 深圳市 003,005,006,007,008,009,010 深圳市,罗湖区,福田区,宝安区,西乡镇,龙华镇,松岗镇 004 天河区 004 天河区 005 罗湖区 005 罗湖区 006 福田区 006 福田区 007 宝安区 007,008,009,010 宝安区,西乡镇,龙华镇,松岗镇 008 西乡镇 008 西乡镇 009 龙华镇 009 龙华镇 010 松岗镇 010 松岗镇(10 行受影响) */drop table tb
use City; go if OBJECT_ID(N'A',N'U') is not null drop table A go create table A ( id int identity(1,1) primary key not null, useid int, parentid int ) go --插入A表测试数据 insert into A select 9,8 union all select 8,6 union all select 6,4 union all select 4,0 go with cte as ( select useid,id,cast(parentid as nvarchar(200)) as myparent from A where useid=4 union all select A.useid,A.id,CAST(cte.myparent+','+cast(cte.useid as nvarchar(10)) as nvarchar(200)) as myparent from A inner join cte on A.parentid=cte.useid ) select useid,myparent as relationship from cte order by useid desc /* (4 行受影响) useid relationship ----------- ----------- 9 0,4,6,8 8 0,4,6 6 0,4 4 0 (4 行受影响)*/
--创建表 create table #tb(sid int,pid int) --插入数据 insert into #tb select 9,8 union all select 8,6 union all select 6,4 union all select 4,0 union all;with temp as ( Select sid,pid,cast(ltrim(pid) as varchar(max)) as relationship From #tb Where #tb.pid=0 union all Select #tb.sid,#tb.pid, cast(temp.relationship+','+convert(varchar(max),temp.sid) as varchar(max)) From temp,#tb Where #tb.pid=temp.sid ) select * From temp/* 4 0 0 6 4 0,4 8 6 0,4,6 9 8 0,4,6,8 */
;with t as (
select userid,parentid,
cast(ltrim(parentid) as varchar(max)) relationship
from table where parentid=0
union all
select t1.userid,t1.parentid,
cast(t.relationship+','+ltrim(userid) as varchar(max))
from table t1, t where t.userid=t1.parentid
)
select userid,relationship from t;
/*
标题:SQL SERVER 2000中查询指定节点及其所有子节点的函数(字符串形式显示)
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2010-02-02
地点:新疆乌鲁木齐
*/--生成测试数据
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(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_cid(id) , '') from tb group by iddrop table tb
drop function f_cid/*
id children
---- ---------------------------------------
001 001,002,003,004,005,006,007,008,009,010
002 002,004
003 003,005,006,007,008,009,010
004 004
005 005
006 006
007 007,008,009,010
008 008
009 009
010 010(所影响的行数为 10 行)
*/
/*
标题:SQL SERVER 2005中查询指定节点及其所有子节点的方法(字符串形式显示)
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2010-02-02
地点:新疆乌鲁木齐
*/create table tb(id varchar(3) , pid varchar(3) , name nvarchar(10))
insert into tb values('001' , null , N'广东省')
insert into tb values('002' , '001' , N'广州市')
insert into tb values('003' , '001' , N'深圳市')
insert into tb values('004' , '002' , N'天河区')
insert into tb values('005' , '003' , N'罗湖区')
insert into tb values('006' , '003' , N'福田区')
insert into tb values('007' , '003' , N'宝安区')
insert into tb values('008' , '007' , N'西乡镇')
insert into tb values('009' , '007' , N'龙华镇')
insert into tb values('010' , '007' , N'松岗镇')
go;with t as
(
select id , cid = id from tb
union all
select t.id , cid = tb.id
from t join tb on tb.pid = t.cid
)
select id , cid = STUFF((SELECT ',' + rtrim(cid) FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , '')
from tb
group by id
order by id
/*
id cid
---- ---------------------------------------
001 001,002,003,005,006,007,008,009,010,004
002 002,004
003 003,005,006,007,008,009,010
004 004
005 005
006 006
007 007,008,009,010
008 008
009 009
010 010(10 行受影响)
*/;with t as
(
select id , name , cid = id , path = cast(name as nvarchar(100)) from tb
union all
select t.id , t.name , cid = tb.id , path = cast(tb.name as nvarchar(100))
from t join tb on tb.pid = t.cid
)
select id , name ,
cid = STUFF((SELECT ',' + rtrim(cid) FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , ''),
path = STUFF((SELECT ',' + path FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , '')
from tb
group by id , name
order by id
/*
id name cid path
---- ---------- ------------------------------------------- ---------------------------------------------------------------------
001 广东省 001,002,003,005,006,007,008,009,010,004 广东省,广州市,深圳市,罗湖区,福田区,宝安区,西乡镇,龙华镇,松岗镇,天河区
002 广州市 002,004 广州市,天河区
003 深圳市 003,005,006,007,008,009,010 深圳市,罗湖区,福田区,宝安区,西乡镇,龙华镇,松岗镇
004 天河区 004 天河区
005 罗湖区 005 罗湖区
006 福田区 006 福田区
007 宝安区 007,008,009,010 宝安区,西乡镇,龙华镇,松岗镇
008 西乡镇 008 西乡镇
009 龙华镇 009 龙华镇
010 松岗镇 010 松岗镇(10 行受影响)
*/drop table tb
use City;
go
if OBJECT_ID(N'A',N'U') is not null drop table A
go
create table A
(
id int identity(1,1) primary key not null,
useid int,
parentid int
)
go
--插入A表测试数据
insert into A
select 9,8 union all
select 8,6 union all
select 6,4 union all
select 4,0
go
with cte as
(
select useid,id,cast(parentid as nvarchar(200)) as myparent from A where useid=4
union all
select A.useid,A.id,CAST(cte.myparent+','+cast(cte.useid as nvarchar(10)) as nvarchar(200)) as myparent from A inner join cte on A.parentid=cte.useid
)
select useid,myparent as relationship from cte order by useid desc
/*
(4 行受影响)
useid relationship
----------- -----------
9 0,4,6,8
8 0,4,6
6 0,4
4 0
(4 行受影响)*/
--创建表
create table #tb(sid int,pid int)
--插入数据
insert into #tb
select 9,8 union all
select 8,6 union all
select 6,4 union all
select 4,0 union all;with temp as
(
Select sid,pid,cast(ltrim(pid) as varchar(max)) as relationship From #tb Where #tb.pid=0
union all
Select #tb.sid,#tb.pid, cast(temp.relationship+','+convert(varchar(max),temp.sid) as varchar(max))
From temp,#tb Where #tb.pid=temp.sid
)
select * From temp/*
4 0 0
6 4 0,4
8 6 0,4,6
9 8 0,4,6,8
*/