/*--按父找子--*/ declare @a table (TC_Id int,TC_PID int,TC_Name varchar(200)) insert @a values(1,0,'中国') insert @a values(2,0,'美国') insert @a values(3,0,'加拿大') insert @a values(4,1,'北京') insert @a values(5,1,'上海') insert @a values(6,1,'江苏') insert @a values(7,6,'苏州') insert @a values(8,7,'常熟') insert @a values(9,6,'南京') insert @a values(10,6,'无锡') insert @a values(11,2,'纽约') insert @a values(12,2,'旧金山')declare @tmp1 table (TC_Id int,TC_PID int,TC_Name varchar(200),lev int) insert @tmp1 select *,1 from @a where tc_ID=1 while @@rowcount>0 insert @tmp1 select a.*,1 from @a a,@tmp1 b where a.tc_pid=b.tc_ID and a.tc_ID not in (select tc_ID from @tmp1) select * from @tmp1
declare @编号 varchar(100) set @编号=2declare @t table(id int,level int) declare @level int set @level=1 insert @t select 编号,@level from 表 where 编号=@编号 while @@rowcount>0 begin set @level=@level+1 insert @t select a.编号,@level from 表 a join @t b on a.隶属=b.ID where b.level=@level-1 endselect * from @t
--下面是数据测试--测试数据 declare @tb table(编号 int,名称 varchar(10),隶属 int) insert into @tb select 1,'aa',0 union all select 2,'bb',1 union all select 3,'c',1 union all select 4,'dd',2 union all select 5,'ee',3 union all select 6,'ww',2 union all select 7,'qq',4 union all select 8,'ff',4--查询处理 declare @编号 varchar(100) set @编号=2declare @t table(id int,level int) declare @level int set @level=1 insert @t select 编号,@level from @tb where 编号=@编号 while @@rowcount>0 begin set @level=@level+1 insert @t select a.编号,@level from @tb a join @t b on a.隶属=b.ID where b.level=@level-1 end--得到结果 select a.* from @tb a join @t b on a.编号=b.id/*--测试结果 编号 名称 隶属 ----------- ---------- ----------- 2 bb 1 4 dd 2 6 ww 2 7 qq 4 8 ff 4(所影响的行数为 5 行) --*/
--为方便处理,可以写成函数--创建函数,得到指定编号及其下属列表 create function f_getcid(@编号 int) returns @t table(id int,level int) as begin declare @level int set @level=1 insert @t select 编号,@level from tb where 编号=@编号 while @@rowcount>0 begin set @level=@level+1 insert @t select a.编号,@level from tb a join @t b on a.隶属=b.ID where b.level=@level-1 end return end go--得到结果 select a.* from tb a join dbo.f_getcid(2) b on a.编号=b.id
--下面是数据测试--测试数据 create table tb(编号 int,名称 varchar(10),隶属 int) insert into tb select 1,'aa',0 union all select 2,'bb',1 union all select 3,'c',1 union all select 4,'dd',2 union all select 5,'ee',3 union all select 6,'ww',2 union all select 7,'qq',4 union all select 8,'ff',4go--创建函数,得到指定编号及其下属列表 create function f_getcid(@编号 int) returns @t table(id int,level int) as begin declare @level int set @level=1 insert @t select 编号,@level from tb where 编号=@编号 while @@rowcount>0 begin set @level=@level+1 insert @t select a.编号,@level from tb a join @t b on a.隶属=b.ID where b.level=@level-1 end return end go--得到结果 select a.* from tb a join dbo.f_getcid(2) b on a.编号=b.idgo --删除测试环境 drop table tb drop function f_getcid/*--测试结果 编号 名称 隶属 ----------- ---------- ----------- 2 bb 1 4 dd 2 6 ww 2 7 qq 4 8 ff 4(所影响的行数为 5 行) --*/
http://expert.csdn.net/Expert/TopicView1.asp?id=2285830
declare @a table (TC_Id int,TC_PID int,TC_Name varchar(200))
insert @a values(1,0,'中国')
insert @a values(2,0,'美国')
insert @a values(3,0,'加拿大')
insert @a values(4,1,'北京')
insert @a values(5,1,'上海')
insert @a values(6,1,'江苏')
insert @a values(7,6,'苏州')
insert @a values(8,7,'常熟')
insert @a values(9,6,'南京')
insert @a values(10,6,'无锡')
insert @a values(11,2,'纽约')
insert @a values(12,2,'旧金山')declare @tmp1 table (TC_Id int,TC_PID int,TC_Name varchar(200),lev int)
insert @tmp1 select *,1 from @a where tc_ID=1
while @@rowcount>0
insert @tmp1 select a.*,1 from @a a,@tmp1 b where a.tc_pid=b.tc_ID and a.tc_ID not in (select tc_ID from @tmp1)
select * from @tmp1
set @编号=2declare @t table(id int,level int)
declare @level int
set @level=1
insert @t select 编号,@level from 表 where 编号=@编号
while @@rowcount>0
begin
set @level=@level+1
insert @t select a.编号,@level from 表 a join @t b on a.隶属=b.ID
where b.level=@level-1
endselect * from @t
declare @tb table(编号 int,名称 varchar(10),隶属 int)
insert into @tb
select 1,'aa',0
union all select 2,'bb',1
union all select 3,'c',1
union all select 4,'dd',2
union all select 5,'ee',3
union all select 6,'ww',2
union all select 7,'qq',4
union all select 8,'ff',4--查询处理
declare @编号 varchar(100)
set @编号=2declare @t table(id int,level int)
declare @level int
set @level=1
insert @t select 编号,@level from @tb where 编号=@编号
while @@rowcount>0
begin
set @level=@level+1
insert @t select a.编号,@level from @tb a join @t b on a.隶属=b.ID
where b.level=@level-1
end--得到结果
select a.* from @tb a join @t b on a.编号=b.id/*--测试结果
编号 名称 隶属
----------- ---------- -----------
2 bb 1
4 dd 2
6 ww 2
7 qq 4
8 ff 4(所影响的行数为 5 行)
--*/
create function f_getcid(@编号 int)
returns @t table(id int,level int)
as
begin
declare @level int
set @level=1
insert @t select 编号,@level from tb where 编号=@编号
while @@rowcount>0
begin
set @level=@level+1
insert @t select a.编号,@level from tb a join @t b on a.隶属=b.ID
where b.level=@level-1
end
return
end
go--得到结果
select a.* from tb a join dbo.f_getcid(2) b on a.编号=b.id
create table tb(编号 int,名称 varchar(10),隶属 int)
insert into tb
select 1,'aa',0
union all select 2,'bb',1
union all select 3,'c',1
union all select 4,'dd',2
union all select 5,'ee',3
union all select 6,'ww',2
union all select 7,'qq',4
union all select 8,'ff',4go--创建函数,得到指定编号及其下属列表
create function f_getcid(@编号 int)
returns @t table(id int,level int)
as
begin
declare @level int
set @level=1
insert @t select 编号,@level from tb where 编号=@编号
while @@rowcount>0
begin
set @level=@level+1
insert @t select a.编号,@level from tb a join @t b on a.隶属=b.ID
where b.level=@level-1
end
return
end
go--得到结果
select a.* from tb a join dbo.f_getcid(2) b on a.编号=b.idgo
--删除测试环境
drop table tb
drop function f_getcid/*--测试结果
编号 名称 隶属
----------- ---------- -----------
2 bb 1
4 dd 2
6 ww 2
7 qq 4
8 ff 4(所影响的行数为 5 行)
--*/
http://expert.csdn.net/Expert/topic/2285/2285830.xml?temp=.235531