首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求一树型查询!解决方案

2012-01-19 
求一树型查询!!AdminPart_IdAdminPart_NameAdminPart_UpId6分站管理员1111超级管理员012北京分站管理员613

求一树型查询!!
AdminPart_Id     AdminPart_Name     AdminPart_UpId

6分站管理员11
11超级管理员0
12           北京分站管理员                         6
13           上海分站管理员                         6
14           北京新闻频道管理员                 12
15           上海新闻频道管理员                   13

要求实现这样的查询     如果     我输入的是       6  
那么结果为

12           北京分站管理员                         6
13           上海分站管理员                         6
14           北京新闻频道管理员                 12
15           上海新闻频道管理员                   13
如果输入的是     13  
则结果为
15           上海新闻频道管理员                   13
如果输入的是     12  
则结果为  
14           北京新闻频道管理员                 12


[解决办法]
if object_id( 'tbTest ') is not null
drop table tbTest
if object_id( 'fnGetChildren ') is not null
drop function fnGetChildren
GO
create table tbTest(AdminPart_Id int,AdminPart_Name varchar(20),AdminPart_UpId int)
insert tbTest
select 6, '分站管理员 ',11 union all
select 11, '超级管理员 ',0 union all
select 12, '北京分站管理员 ', 6 union all
select 13, '上海分站管理员 ', 6 union all
select 14, '北京新闻频道管理员 ', 12 union all
select 15, '上海新闻频道管理员 ', 13
GO
create function fnGetChildren(@id int)
returns @t table(AdminPart_Id int,AdminPart_Name varchar(20),AdminPart_UpId int)
as
begin
insert @t select AdminPart_Id,AdminPart_Name,AdminPart_UpId from tbTest where AdminPart_UpId = @id
while @@rowcount > 0
insert @t select a.AdminPart_Id,a.AdminPart_Name,a.AdminPart_UpId from tbTest as a
inner join @t as b on a.AdminPart_UpId = b.AdminPart_Id
and a.AdminPart_Id not in (select AdminPart_Id from @t)
return
end
GO
----查询
declare @id int
set @id = 6
select * from dbo.fnGetChildren(@id)

----清除测试环境
drop table tbTest
drop function fnGetChildren

/*结果:
AdminPart_Id AdminPart_Name AdminPart_UpId
------------ -------------------- --------------
12 北京分站管理员 6
13 上海分站管理员 6
14 北京新闻频道管理员 12
15 上海新闻频道管理员 13
*/
[解决办法]
create table test(AdminPart_Id int,AdminPart_Name varchar(30),AdminPart_UpId int)
insert test select 6, '分站管理员 ',11
union all select 11, '超级管理员 ',0
union all select 12, '北京分站管理员 ',6
union all select 13, '上海分站管理员 ',6
union all select 14, '北京新闻频道管理员 ',12
union all select 15, '上海新闻频道管理员 ',13
go

create function F_GetChild(@ID int)


returns @Tree table(ID int,Level int)
as
begin
declare @Level int
set @Level=1
insert @Tree select @ID,@Level
while @@rowcount> 0
begin
set @Level=@Level+1
insert @Tree select a.AdminPart_Id,@Level
from test a,@Tree b
where a.AdminPart_UpId=b.ID
and b.Level=@Level-1
end
return
end
go

--6
select a.ID,b.AdminPart_Name,b.AdminPart_UpId from dbo.F_GetChild(6) as a left join test b
on a.ID=b.AdminPart_Id
where a.ID <> 6

--12
select a.ID,b.AdminPart_Name,b.AdminPart_UpId from dbo.F_GetChild(12) as a left join test b
on a.ID=b.AdminPart_Id
where a.ID <> 12

--13
select a.ID,b.AdminPart_Name,b.AdminPart_UpId from dbo.F_GetChild(13) as a left join test b
on a.ID=b.AdminPart_Id
where a.ID <> 13


drop table test
drop function F_GetChild

ID AdminPart_Name AdminPart_UpId
----------- ------------------------------ --------------
12 北京分站管理员 6
13 上海分站管理员 6
14 北京新闻频道管理员 12
15 上海新闻频道管理员 13

(所影响的行数为 4 行)

ID AdminPart_Name AdminPart_UpId
----------- ------------------------------ --------------
14 北京新闻频道管理员 12

(所影响的行数为 1 行)

ID AdminPart_Name AdminPart_UpId
----------- ------------------------------ --------------
15 上海新闻频道管理员 13

(所影响的行数为 1 行)

热点排行
Bad Request.