如有表用户
用户
A
B
C
D
E
F
然后有用户下上级
下级 上级
B A
C A
D B
E B
F C
------------
然后
要查询出用户B和他所有的下级如结果为
B
D
E
要查询出用户C和他所有的下级如结果为
C
F要查询出用户F和他所有的上级如结果为
F
C
A谢谢
用户
A
B
C
D
E
F
然后有用户下上级
下级 上级
B A
C A
D B
E B
F C
------------
然后
要查询出用户B和他所有的下级如结果为
B
D
E
要查询出用户C和他所有的下级如结果为
C
F要查询出用户F和他所有的上级如结果为
F
C
A谢谢
select 'b' as 用户
union all
select 下级 as 用户
from 用户下上级 a,cte b
where a.上级 = b.用户
)
select * from cte
--生成测试数据
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 行)
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
标题:SQL SERVER 2000中查询指定节点及其所有子节点的函数(表格形式显示)
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间: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@@ROWCOUNT:返回受上一语句影响的行数。
返回类型:integer。
注释:任何不返回行的语句将这一变量设置为 0 ,如 IF 语句。
示例:下面的示例执行 UPDATE 语句并用 @@ROWCOUNT 来检测是否有发生更改的行。UPDATE authors SET au_lname = 'Jones' WHERE au_id = '999-888-7777'
IF @@ROWCOUNT = 0
print 'Warning: No rows were updated'结果:(所影响的行数为 0 行)
Warning: No rows were updated/*
标题: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'松岗镇')
goDECLARE @ID VARCHAR(3)--查询ID = '001'的所有子节点
SET @ID = '001'
;WITH T AS
(
SELECT ID , PID , NAME
FROM TB
WHERE ID = @ID
UNION ALL
SELECT A.ID , A.PID , A.NAME
FROM TB AS A JOIN T AS B ON A.PID = B.ID
)
SELECT * FROM T ORDER BY 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 行受影响)
*/--查询ID = '002'的所有子节点
SET @ID = '002'
;WITH T AS
(
SELECT ID , PID , NAME
FROM TB
WHERE ID = @ID
UNION ALL
SELECT A.ID , A.PID , A.NAME
FROM TB AS A JOIN T AS B ON A.PID = B.ID
)
SELECT * FROM T ORDER BY ID
/*
ID PID NAME
---- ---- ----------
002 001 广州市
004 002 天河区(2 行受影响)
*/--查询ID = '003'的所有子节点
SET @ID = '003'
;WITH T AS
(
SELECT ID , PID , NAME
FROM TB
WHERE ID = @ID
UNION ALL
SELECT A.ID , A.PID , A.NAME
FROM TB AS A JOIN T AS B ON A.PID = B.ID
)
SELECT * FROM T ORDER BY ID
/*
ID PID NAME
---- ---- ----------
003 001 深圳市
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇(7 行受影响)
*/drop table tb--注:除ID值不一样外,三个SQL语句是一样的。
create table tu(id varchar(10))
insert into tu
select 'A' union all
select 'B' union all
select 'C' union all
select 'D' union all
select 'E' union all
select 'F'
gocreate table tb(id varchar(10),parentid varchar(10))
insert into tb
select 'B','A' union all
select 'C','A' union all
select 'D','B' union all
select 'E','B' union all
select 'F','C'
godeclare @id varchar(10)
set @id = 'B';with ct as
(
select a.id,b.parentid
from tu a left join tb b on a.id = b.id
),cte as
(
select id,parentid,'本级ID' as TFlag from ct where id = @id
union all
select a.id,a.parentid,'下级ID'
from ct a join cte as f on a.parentid = f.id
where a.parentid is not null
),cta as
(
select id,parentid,'本级ID' as TFlag from ct where id = @id
union all
select a.id,a.parentid,'上级ID'
from ct a join cta as f on f.parentid = a.id
where a.id is not null
)select * from cte
union
select * from ctadrop table tb,tu/****************id parentid TFlag
---------- ---------- ------
A NULL 上级ID
B A 本级ID
D B 下级ID
E B 下级ID(4 行受影响)
(
select 'b' as 用户 from 用户表
union all
select 下级 as 用户
from 用户下上级 a,f b where a.上级 = b.用户
)
select * from f
if object_id('用户下上级表') is not null
drop table 用户下上级表
go
create table 用户下上级表
(
下级 varchar(10),
上级 varchar(10)
)
go
insert into 用户下上级表
select 'B','A' union all
select 'C','A' union all
select 'D','B' union all
select 'E','B' union all
select 'F','C'
go
--用公用表表达式的递归查询方法
;with cte as
(
select * from 用户下上级表 where 下级='B' --这里换成你想查的用户
union all
select a.* from 用户下上级表 a inner join cte b on a.上级=b.下级
)
select 下级 from cte
/*
下级
----------
B
D
E(3 行受影响)
*/
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'
go
/*
declare @u varchar(10),@l varchar(10)
set @u='B'
set @l='下级'
*/create procedure getuser
(@u varchar(10),@l varchar(10))
as
;with cte as(
select top 1 * from tb where @u=下级
union all
select a.* from tb a inner join cte b on
(case when @l='下级' then b.下级 else b.上级 end)=(case when @l='下级' then a.上级 else a.下级 end)
)
select 下级 as 用户 from cte
union
select 上级 from cte where @l='上级'
go
exec getuser 'B','下级'
/
用户
----------
B
D
E(3 行受影响)
*/
exec getuser 'C','下级'
/*
用户
----------
C
F(2 行受影响)
*/
exec getuser 'F','上级'
/*
用户
----------
A
C
F(3 行受影响)*/
go
drop table tb
drop procedure getuser
create table tb(下级 varchar(10),上级 varchar(10))
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'
go
/*
declare @u varchar(10),@l varchar(10)
set @u='B'
set @l='下级'
*/create procedure getuser
(@u varchar(10),@l varchar(10))
as
;with cte as(
select top 1 * from tb where @u=下级
union all
select a.* from tb a inner join cte b on
(case when @l='下级' then b.下级 else b.上级 end)=(case when @l='下级' then a.上级 else a.下级 end)
)
select 下级 as 用户 from cte
union
select 上级 from cte where @l='上级'
go
exec getuser 'B','下级'
/*
用户
----------
B
D
E(3 行受影响)
*/
exec getuser 'C','下级'
/*
用户
----------
C
F(2 行受影响)
*/
exec getuser 'F','上级'
/*
用户
----------
A
C
F(3 行受影响)*/
go
drop table tb
drop procedure getuser