先给个基础你看下
create table tbb([no] int,[code] varchar(2),[value] int)
insert tbb
select 1,'c1',2 union all
select 1,'c1',1 union all
select 2,'c2',1 union all
select 1,'c1',8 union all
select 2,'c2',5
drop function dbo.fc_v
--(1)
create function dbo.fc_v(@id varchar(2))
returns varchar(200)
as
begin
declare @i varchar(100)
set @i=''
select @i=@i+','+cast([value] as varchar) from tbb where [code]=@id
return stuff(@i,1,1,'')
endselect [code],dbo.fc_v([code]) from tbb group by [code]
create table tbb([no] int,[code] varchar(2),[value] int)
insert tbb
select 1,'c1',2 union all
select 1,'c1',1 union all
select 2,'c2',1 union all
select 1,'c1',8 union all
select 2,'c2',5
drop function dbo.fc_v
--(1)
create function dbo.fc_v(@id varchar(2))
returns varchar(200)
as
begin
declare @i varchar(100)
set @i=''
select @i=@i+','+cast([value] as varchar) from tbb where [code]=@id
return stuff(@i,1,1,'')
endselect [code],dbo.fc_v([code]) from tbb group by [code]
建议看看邹老大上面的实现方法。你写的这个太麻烦了
标题:查询各节点的父路径函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*//*
原始数据及要求结果如下:
--食品
--水果
--香蕉
--苹果
--蔬菜
--青菜
id pid name
----------- ----------- --------------------
1 0 食品
2 1 水果
3 1 蔬菜
4 2 香蕉
5 2 苹果
6 3 青菜要求得到各节点的父路径即如下结果:
id pid name 路径
--- --- ----- ---------------
1 0 食品 食品
2 1 水果 食品,水果
3 1 蔬菜 食品,蔬菜
4 2 香蕉 食品,水果,香蕉
5 2 苹果 食品,水果,苹果
6 3 青菜 食品,蔬菜,青菜
*/create table tb (id int , pid int , name nvarchar(20))
insert into tb values(1 , 0 , '食品')
insert into tb values(2 , 1 , '水果')
insert into tb values(3 , 1 , '蔬菜')
insert into tb values(4 , 2 , '香蕉')
insert into tb values(5 , 2 , '苹果')
insert into tb values(6 , 3 , '青菜')
go--查询各节点的父路径函数
create function f_pid(@id int) returns varchar(100)
as
begin
declare @re_str as varchar(100)
set @re_str = ''
select @re_str = name from tb where id = @id
while exists (select 1 from tb where id = @id and pid <> 0)
begin
select @id = b.id , @re_str = b.name + ',' + @re_str from tb a , tb b where a.id = @id and a.pid = b.id
end
return @re_str
end
goselect * , dbo.f_pid(id) 路径 from tb order by iddrop table tb
drop function f_pid
结构如下:
id ---int
parentid---int 上层的父节点
sname--varchar(50) 如有以下数据:
id parentid sname
1 -1 根节点
2 1 节点1
3 1 节点2
4 2 节点3
5 2 节点4
6 3 节点5
7 3 节点6
7 6 节点7 假如:我要得到某个节点下所有的最底层的节点数据怎么实现:
如:我要得到id=3 的所有最底层的数据是:
id parentid sname
7 3 节点6
7 6 节点7
请大家多多帮忙,谢谢! create table tb(id int, parentid int, sname varchar(10))
insert into tb values(1 , -1 , '根节点')
insert into tb values(2 , 1 , '节点1')
insert into tb values(3 , 1 , '节点2')
insert into tb values(4 , 2 , '节点3')
insert into tb values(5 , 2 , '节点4')
insert into tb values(6 , 3 , '节点5')
insert into tb values(7 , 3 , '节点6')
insert into tb values(7 , 6 , '节点7')
go--查询指定节点及其所有子节点的函数
CREATE FUNCTION f_Cid(@ID int) RETURNS @t_Level TABLE(ID int,Level int)
AS
BEGIN
DECLARE @Level int
SET @Level=1
INSERT @t_Level SELECT @ID,@Level
WHILE @@ROWCOUNT>0
BEGIN
SET @Level=@Level+1
INSERT @t_Level SELECT a.ID,@Level
FROM tb a,@t_Level b
WHERE a.parentid=b.ID
AND b.Level=@Level-1
END
RETURN
END
GO SELECT distinct a.* FROM tb a,f_Cid(3) b WHERE a.ID=b.ID and a.id not in(select parentid from tb)drop table tb
drop function dbo.f_cid/*
id parentid sname
----------- ----------- ----------
7 3 节点6
7 6 节点7(所影响的行数为 2 行)
*/
--2005
;WITH
TREE AS(
SELECT * FROM T
WHERE parentid = 3 -- 要查询的父 id
UNION ALL
SELECT T * FROM T, TREE
WHERE T.parentid = TREE.id
)
SELECT * FROM TREE
max(parentid)未必与传入的id有联系, 看不懂这里为什么这么写。这样,无论传什么进去,其实都会把所有的都执行一次,update时才过滤, 但是循环次数是明显加大了。不推荐这种方式处理,不想去理清它的路子,所以不讲它的思路了.
常规做法是跟据传入的id,找parentid(或者子id,反向), 找到后,看有无子,再继续,用某种方式来代替递规调用.
begin
select @id = b.id , @re_str = b.name + ',' + @re_str from tb a , tb b where a.id = @id and a.pid = b.id
end
return @re_str
麻烦啦