create table taskplan(
id int primary key,
parentid int ,
pname varchar(60));create table task(
id int primary key,
pid int ,
tname varchar(60));insert into taskplan values(1,2,'父任务1');
insert into taskplan values(2,3,'父任务2');
insert into taskplan values(3,4,'父任务3');
insert into taskplan values(4,4,'父任务4');insert into taskplan values(5,6,'任务2_1');
insert into taskplan values(6,6,'任务2_2');insert into task values(1,1,'任务1');
insert into task values(2,5,'任务2');select * from task;
select * from taskplan;
比方说是这张表 我要的查询结果是:
id tname pname
1 任务1 父任务1>父任务2>父任务3>父任务4
2 任务2 父任务2_1>父任务2_2前提是你不知道父节点的个数和父节点的ID,
希望哪位好心人会的帮忙写哈,这个东西都困扰我几天了,谢谢了!!!
id int primary key,
parentid int ,
pname varchar(60));create table task(
id int primary key,
pid int ,
tname varchar(60));insert into taskplan values(1,2,'父任务1');
insert into taskplan values(2,3,'父任务2');
insert into taskplan values(3,4,'父任务3');
insert into taskplan values(4,4,'父任务4');insert into taskplan values(5,6,'任务2_1');
insert into taskplan values(6,6,'任务2_2');insert into task values(1,1,'任务1');
insert into task values(2,5,'任务2');select * from task;
select * from taskplan;
比方说是这张表 我要的查询结果是:
id tname pname
1 任务1 父任务1>父任务2>父任务3>父任务4
2 任务2 父任务2_1>父任务2_2前提是你不知道父节点的个数和父节点的ID,
希望哪位好心人会的帮忙写哈,这个东西都困扰我几天了,谢谢了!!!
/*
标题:SQL SERVER 2000中查询指定节点及其所有父节点的函数(字符串形式显示)
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2010-02-02
地点:新疆乌鲁木齐
*/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_pid1(@id varchar(3)) 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 is not null)
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
go
--查询各节点的父路径函数(从子到父)
create function f_pid2(@id varchar(3)) 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 is not null)
begin
select @id = b.id , @re_str = @re_str + ',' + b.name from tb a , tb b where a.id = @id and a.pid = b.id
end
return @re_str
end
goselect * ,
dbo.f_pid1(id) [路径(从父到子)] ,
dbo.f_pid2(id) [路径(从子到父)]
from tb order by iddrop function f_pid1 , f_pid2
drop table tb/*
id pid name 路径(从父到子) 路径(从子到父)
---- ---- ------ --------------------------- ----------------------------
001 NULL 广东省 广东省 广东省
002 001 广州市 广东省,广州市 广州市,广东省
003 001 深圳市 广东省,深圳市 深圳市,广东省
004 002 天河区 广东省,广州市,天河区 天河区,广州市,广东省
005 003 罗湖区 广东省,深圳市,罗湖区 罗湖区,深圳市,广东省
006 003 福田区 广东省,深圳市,福田区 福田区,深圳市,广东省
007 003 宝安区 广东省,深圳市,宝安区 宝安区,深圳市,广东省
008 007 西乡镇 广东省,深圳市,宝安区,西乡镇 西乡镇,宝安区,深圳市,广东省
009 007 龙华镇 广东省,深圳市,宝安区,龙华镇 龙华镇,宝安区,深圳市,广东省
010 007 松岗镇 广东省,深圳市,宝安区,松岗镇 松岗镇,宝安区,深圳市,广东省(所影响的行数为 10 行)
*/SQL code
/*
标题: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'松岗镇')
go;with t as
(
select id , pid = id from tb
union all
select t.id , pid = tb.pid from t inner join tb on t.pid = tb.id
)
select id ,
[路径(从父到子)] = STUFF((SELECT ',' + pid FROM t WHERE id = tb.id order by t.id , t.pid FOR XML PATH('')) , 1 , 1 , ''),
[路径(从子到父)] = STUFF((SELECT ',' + pid FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , '')
from tb
group by id
order by id
/*
id 路径(从父到子) 路径(从子到父)
---- --------------- ---------------
001 001 001
002 001,002 002,001
003 001,003 003,001
004 001,002,004 004,002,001
005 001,003,005 005,003,001
006 001,003,006 006,003,001
007 001,003,007 007,003,001
008 001,003,007,008 008,007,003,001
009 001,003,007,009 009,007,003,001
010 001,003,007,010 010,007,003,001(10 行受影响)
*/
;with t as
(
select id , name , pid = id , path = cast(name as nvarchar(100)) from tb
union all
select t.id , t.name , pid = tb.pid , path = cast(tb.name as nvarchar(100)) from t join tb on tb.id = t.pid
)
select id ,
name ,
[路径(从父到子)_1] = pid1,
[路径(从父到子)_2] = reverse(substring(reverse(path1) , charindex(',' , reverse(path1)) + 1 , len(path1))) ,
[路径(从子到父)_1] = pid2,
[路径(从子到父)_2] = substring(path2 , charindex(',' , path2) + 1 , len(path2)) from
(
select id , name ,
pid1 = STUFF((SELECT ',' + pid FROM t WHERE id = tb.id order by t.id , t.pid FOR XML PATH('')) , 1 , 1 , ''),
pid2 = STUFF((SELECT ',' + pid FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , ''),
path1 = STUFF((SELECT ',' + path FROM t WHERE id = tb.id order by t.id , t.pid FOR XML PATH('')) , 1 , 1 , ''),
path2 = STUFF((SELECT ',' + path FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , '')
from tb
group by id , name
) m
order by id
/*
id name 路径(从父到子)_1 路径(从父到子)_2 路径(从子到父)_1 路径(从子到父)_2
---- ------ ---------------- --------------------------- ---------------- ---------------------------
001 广东省 001 广东省 001 广东省
002 广州市 001,002 广东省,广州市 002,001 广州市,广东省
003 深圳市 001,003 广东省,深圳市 003,001 深圳市,广东省
004 天河区 001,002,004 广东省,广州市,天河区 004,002,001 天河区,广州市,广东省
005 罗湖区 001,003,005 广东省,深圳市,罗湖区 005,003,001 罗湖区,深圳市,广东省
006 福田区 001,003,006 广东省,深圳市,福田区 006,003,001 福田区,深圳市,广东省
007 宝安区 001,003,007 广东省,深圳市,宝安区 007,003,001 宝安区,深圳市,广东省
008 西乡镇 001,003,007,008 广东省,深圳市,宝安区,西乡镇 008,007,003,001 西乡镇,宝安区,深圳市,广东省
009 龙华镇 001,003,007,009 广东省,深圳市,宝安区,龙华镇 009,007,003,001 龙华镇,宝安区,深圳市,广东省
010 松岗镇 001,003,007,010 广东省,深圳市,宝安区,松岗镇 010,007,003,001 松岗镇,宝安区,深圳市,广东省(10 行受影响)
*/
create table taskplan(
id int primary key,
parentid int ,
pname varchar(60));create table task(
id int primary key,
pid int ,
tname varchar(60));
set nocount on
insert into taskplan values(1,2,'父任务1');
insert into taskplan values(2,3,'父任务2');
insert into taskplan values(3,4,'父任务3');
insert into taskplan values(4,4,'父任务4');insert into taskplan values(5,6,'任务2_1');
insert into taskplan values(6,6,'任务2_2');insert into task values(1,1,'任务1');
insert into task values(2,5,'任务2');
set nocount off
go;with AcHerat as
(
select a.id aid,b.id,b.parentid from task a join taskplan b on a.pid = b.id
union all
select a.aid,b.id,b.parentid from AcHerat a join taskplan b on a.parentid = b.id and a.id + 1 = b.id
)select b.*,stuff((select N'-'+c.pname from taskplan c join AcHerat b on c.id = b.id and b.aid = a.aid for xml path('')),1,1,'')
from AcHerat a join task b on a.aid = b.id
group by a.aid,b.id,b.pid,b.tnamedrop table task,taskplan/******************id pid tname
----------- ----------- ------------------------------------------------------------ ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 1 任务1 父任务1-父任务2-父任务3-父任务4
2 5 任务2 任务2_1-任务2_2(2 行受影响)
(
select a.id aid,b.id,b.parentid from task a join taskplan b on a.pid = b.id
union all
select a.aid,b.id,b.parentid from AcHerat a join taskplan b on a.parentid = b.id and a.id + 1 = b.id
)select
b.*,stuff((select N'-'+c.pname from taskplan c join f b on c.id = b.id and b.aid = a.aid for xml path('')),1,1,'')
from
f a join task b
on
a.aid = b.id
group by
a.aid,b.id,b.pid,b.tname