select [text] from [bom] where [nodeid] in (
select [parentid] from [bom] where [nodeid] in(
select [parentid] from [bom] where [nodeid] in(
select [parentid] from [bom] where [nodeid]='8')))
union all
select [text] from [bom] where [nodeid] in(
select [parentid] from [bom] where [nodeid] in(
select [parentid] from [bom] where [nodeid]='8'))
union all
select [text] from [bom] where [nodeid] in(
select [parentid] from [bom] where [nodeid]='8')
union all
select [text] from [bom] where [nodeid]='8'
Create function Ft_Bom(@nodeID int)
returns @tb1 table(NodeID int,Text1 varchar(20),ParentID int)
AS
begin
insert into @tb1 select NodeID,Text,ParentID from bom where nodeID=@nodeID
while @@rowcount>0
insert into @tb
select NodeID,Text,ParentID
from bom
where NodeID in(select ParentID from @tb1)and ParentID not in(select ParentID from @tb1)
return
end
--//
select * from ft_bom(8)
create table tb(NodeID int,Text varchar(20),ParentID int)
insert into tb select 1 ,'物流公司',0
insert into tb select 2 ,'销售部 ',1
insert into tb select 3 ,'片区1 ',2
insert into tb select 4 ,'片区2 ',2
insert into tb select 5 ,'片区3 ',2
insert into tb select 6 ,'片区4 ',2
insert into tb select 7 ,'片区5 ',2
insert into tb select 8 ,'片区5-1 ',7
insert into tb select 9 ,'片区5-2 ',7
insert into tb select 10,'市场部 ',1
insert into tb select 11,'财务部 ',1
insert into tb select 12,'科技公司',0
insert into tb select 13,'软件部 ',10
insert into tb select 14,'测试部 ',10
insert into tb select 15,'测试1部 ',12
create function f_str(@n_id int)
returns @re table(ParentID int,Text varchar(20),level int)
as
begin
declare @l int
set @l=0
insert @re select parentid,text, @l from tb
where nodeid=@n_id
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.parentid,a.text,@l
from tb a,@re b
where a.nodeid=b.parentid and b.level=@l-1
end
return
end
DECLARE @result nvarchar(4000)
DECLARE @NodeID int
SET @result=''
SET @NodeID=8
SELECT @result=Text + ',' FROM [TABLE] WHERE NodeID=@NodeID
WHILE @@rowcount>0
BEGIN
SELECT @NodeID=ParentID FROM [TABLE] WHERE NodeID=@NodeID
SELECT @result=@result + Text + ',' FROM [TABLE] WHERE NodeID=@NodeID
END
SELECT @result
create table BOM(NodeID int,Text varchar(20),ParentID int)
insert into BOM select 1 ,'物流公司',0
insert into BOM select 2 ,'销售部 ',1
insert into BOM select 3 ,'片区1 ',2
insert into BOM select 4 ,'片区2 ',2
insert into BOM select 5 ,'片区3 ',2
insert into BOM select 6 ,'片区4 ',2
insert into BOM select 7 ,'片区5 ',2
insert into BOM select 8 ,'片区5-1 ',7
insert into BOM select 9 ,'片区5-2 ',7
insert into BOM select 10,'市场部 ',1
insert into BOM select 11,'财务部 ',1
insert into BOM select 12,'科技公司',0
insert into BOM select 13,'软件部 ',10
insert into BOM select 14,'测试部 ',10
insert into BOM select 15,'测试1部 ',12
go
create function f_str(@NodeID int)
returns varchar(8000)
as
begin
declare @ret varchar(8000),@ParentID int
select @ret=Text,@ParentID=ParentID from BOM where NodeID=@NodeID
while @@rowcount<>0
begin
set @NodeID=@ParentID
select @ret=Text+','+@ret,@ParentID=ParentID from BOM where NodeID=@NodeID
end
return @ret
end
go
select dbo.f_str(NodeID) from BOM where Text='片区5-1'
go