;with AcHerat as ( select 名称,分类 from tb where 名称 = 'ee' union all select a.名称,a.分类 from tb a join AcHerat b on a.名称 = b.分类 )select stuff((select '-'+名称 from AcHerat order by 名称 for xml path('')),1,1,'')
use weimeicreate table #tb(tname varchar(10), ttype varchar(10)) insert #tb select 'tt','' union all select 'aa','' union all select 'bb','aa' union all select 'cc','bb' union all select 'dd','cc' union all select 'ee','dd' union all select 'mm','tt'select * from #tb where tname<'ee' --tname ttype --aa --bb aa --cc bb --dd cc
如果楼主是2000,可以参照这个例子。 create table tb (名称 varchar(10),性别 varchar(10),二级分类 varchar(10)) insert into tb select 'a1','男人',null union all select 'a2','女人',null union all select 'b1',null,'a1' union all select 'b2',null,'a1' union all select 'b3',null,'a2' union all select 'b4',null,'a2' union all select 'c1',null,'b1' union all select 'c2',null,'b2' union all select 'd1',null,'c1' union all select 'd2',null,'c2' go --2000 create function f_getCode(@code varchar(20)) returns @re table(名称 varchar(10),[level] int) as begin declare @l int set @l = 1 insert @re select 名称,@l from tb where 性别 = @code while @@rowcount>0 begin set @l=@l+1 insert @re select a.名称,@l from @re t join tb a on t.名称 = a.二级分类 where t.[level]=@l-1 end return end go ;with AcHerat as ( select 名称,二级分类 from tb where 性别 = N'男人' union all select a.名称,a.二级分类 from tb a join AcHerat b on a.二级分类 = b.名称 )select * from AcHeratselect * from dbo.f_getCode(N'男人')drop function f_getCode drop table tb 字符串合并的可以参考这些资料: 有表tb, 如下: id value ----- ------ 1 aa 1 bb 2 aaa 2 bbb 2 ccc 需要得到结果: id values ------ ----------- 1 aa,bb 2 aaa,bbb,ccc 即, group by id, 求 value 的和(字符串相加)1. 旧的解决方法-- 1. 创建处理函数 CREATE FUNCTION dbo.f_str(@id int) RETURNS varchar(8000) AS BEGIN DECLARE @r varchar(8000) SET @r = '' SELECT @r = @r + ',' + value FROM tb WHERE id=@id RETURN STUFF(@r, 1, 1, '') END GO -- 调用函数SELECt id, values=dbo.f_str(id) FROM tb GROUP BY id-- 2. 新的解决方法 -- 示例数据 DECLARE @t TABLE(id int, value varchar(10)) INSERT @t SELECT 1, 'aa' UNION ALL SELECT 1, 'bb' UNION ALL SELECT 2, 'aaa' UNION ALL SELECT 2, 'bbb' UNION ALL SELECT 2, 'ccc'-- 查询处理 SELECT * FROM( SELECT DISTINCT id FROM @t )A OUTER APPLY( SELECT [values]= STUFF(REPLACE(REPLACE( ( SELECT value FROM @t N WHERE id = A.id FOR XML AUTO ), '<N value="', ','), '"/>', ''), 1, 1, '') )N/*--结果 id values ----------- ---------------- 1 aa,bb 2 aaa,bbb,ccc (2 行受影响) --*/--各种字符串分函数--3.3.1 使用游标法进行字符串合并处理的示例。 --处理的数据 CREATE TABLE tb(col1 varchar(10),col2 int) INSERT tb SELECT 'a',1 UNION ALL SELECT 'a',2 UNION ALL SELECT 'b',1 UNION ALL SELECT 'b',2 UNION ALL SELECT 'b',3--合并处理 --定义结果集表变量 DECLARE @t TABLE(col1 varchar(10),col2 varchar(100))--定义游标并进行合并处理 DECLARE tb CURSOR LOCAL FOR SELECT col1,col2 FROM tb ORDER BY col1,col2 DECLARE @col1_old varchar(10),@col1 varchar(10),@col2 int,@s varchar(100) OPEN tb FETCH tb INTO @col1,@col2 SELECT @col1_old=@col1,@s='' WHILE @@FETCH_STATUS=0 BEGIN IF @col1=@col1_old SELECT @s=@s+','+CAST(@col2 as varchar) ELSE BEGIN INSERT @t VALUES(@col1_old,STUFF(@s,1,1,'')) SELECT @s=','+CAST(@col2 as varchar),@col1_old=@col1 END FETCH tb INTO @col1,@col2 END INSERT @t VALUES(@col1_old,STUFF(@s,1,1,'')) CLOSE tb DEALLOCATE tb --显示结果并删除测试数据 SELECT * FROM @t DROP TABLE tb /*--结果 col1 col2 ---------- ----------- a 1,2 b 1,2,3 --*/ GO /*==============================================*/ --3.3.2 使用用户定义函数,配合SELECT处理完成字符串合并处理的示例 --处理的数据 CREATE TABLE tb(col1 varchar(10),col2 int) INSERT tb SELECT 'a',1 UNION ALL SELECT 'a',2 UNION ALL SELECT 'b',1 UNION ALL SELECT 'b',2 UNION ALL SELECT 'b',3 GO--合并处理函数 CREATE FUNCTION dbo.f_str(@col1 varchar(10)) RETURNS varchar(100) AS BEGIN DECLARE @re varchar(100) SET @re='' SELECT @re=@re+','+CAST(col2 as varchar) FROM tb WHERE col1=@col1 RETURN(STUFF(@re,1,1,'')) END GO--调用函数 SELECT col1,col2=dbo.f_str(col1) FROM tb GROUP BY col1 --删除测试 DROP TABLE tb DROP FUNCTION f_str /*--结果 col1 col2 ---------- ----------- a 1,2 b 1,2,3 --*/ GO
---创建表 if OBJECT_ID(N'Tbl1',N'U') is not null drop table Tbl1 go create table Tbl1 ( 名称 varchar(10) not null, 分类 varchar(10) ) goinsert into Tbl1 select 'tt','' union all select 'aa','' union all select 'bb','aa' union all select 'cc','bb' union all select 'dd','cc' union all select 'ee','dd' union all select 'mm','tt' declare @a nvarchar(max); -------cte递归 with cte as ( SELECT 名称,分类 FROM Tbl1 where 名称 = 'ee' UNION ALL select a.名称,a.分类 FROM Tbl1 a inner join cte b on a.名称 = b.分类 ) select @a = case isnull(@a, '') when '' then '' else @a + '-' end + 名称 from cte order by 名称 select @a
create table tb(tname varchar(10), ttype varchar(10)) insert tb select 'tt','' union all select 'aa','' union all select 'bb','aa' union all select 'cc','bb' union all select 'dd','cc' union all select 'ee','dd' union all select 'mm','tt' go--2005 ;with AcHerat as ( select tname,ttype from tb where tname = 'ee' union all select a.tname,a.ttype from tb a join AcHerat b on a.tname = b.ttype )select stuff((select '-'+tname from AcHerat order by tname for xml path('')),1,1,'')drop table tb/**************---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- aa-bb-cc-dd-ee
create table tb(tname varchar(10), ttype varchar(10)) insert tb select 'tt','' union all select 'aa','' union all select 'bb','aa' union all select 'cc','bb' union all select 'dd','cc' union all select 'ee','dd' union all select 'mm','tt' go--2000 create function f_getCode(@code varchar(20)) returns @re table(tname varchar(10),ttype varchar(10),[level] int) as begin declare @l int set @l = 1 insert @re select tname,ttype,@l from tb where tname = @code while @@rowcount>0 begin set @l=@l+1 insert @re select a.tname,a.ttype,@l from @re t join tb a on t.ttype = a.tname where t.[level]=@l-1 end return end go declare @str varchar(4000) select @str = isnull(@str+'-','')+tname from dbo.f_getCode('ee') order by [level] descselect @strdrop function f_getCode drop table tb/***************---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- aa-bb-cc-dd-ee
/* 标题: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 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 行受影响) */
晕,我说呢,原来是要这个啊!aa-bb-cc-dd 顶这个
;with AcHerat as ( select tname,ttype from tb where tname = 'ee' union all select a.tname,a.ttype from tb a join AcHerat b on a.tname = b.ttype )select stuff((select '-'+tname from AcHerat order by tname for xml path('')),1,1,'')
楼主如果不要求ee出来,那么查询里加个where条件,where 名称 <> ee 就可以。
;with AcHerat as ( select tname,ttype from tb where tname = 'ee' union all select a.tname,a.ttype from tb a join AcHerat b on a.tname = b.ttype )select stuff((select '-'+tname from AcHerat order by tname for xml path('')),1,1,'') 这个就可以了。
名称 分类 tt aa bb aa cc bb dd cc ee dd mm tt 无限分类...create table #tb(名称 varchar(10), 分类 varchar(10)) insert #tb select 'tt','' union all select 'aa','' union all select 'bb','aa' union all select 'cc','bb' union all select 'dd','cc' union all select 'ee','dd' union all select 'mm','tt'with cte as (select 名称,分类 from #tb where 名称='ee' union all select #tb.名称,#tb.分类 from #tb,cte where #tb.名称=cte.分类) select stuff((select '-'+名称 from cte order by 名称 for xml path('')),1,1,'')/* ----------------- aa-bb-cc-dd-ee(1 行受影响) */
create table tb(名称 varchar(10), 分类 varchar(10)) insert tb select 'tt','' union all select 'aa','' union all select 'bb','aa' union all select 'cc','bb' union all select 'dd','cc' union all select 'ee','dd' union all select 'mm','tt'create function f_str(@名称 varchar(20)) returns @table table(名称 varchar(10),分类 varchar(10),[level] int) as begin declare @i int set @i = 1 insert @table select 名称,分类,@i from tb where 名称 = @名称 while @@rowcount>0 begin set @i=@i+1 insert @table select a.名称,a.分类,@i from @table t join tb a on t.分类 = a.名称 where t.[level]=@i-1 end return end go declare @sql varchar(8000) select @sql = isnull(@sql+'-','')+名称 from dbo.f_str('ee') where 名称<>'ee' order by [level] descselect @sql/* ------------ aa-bb-cc-dd */
create table tb(tname varchar(10), ttype varchar(10)) insert tb select '库存现金','' union all select '存现金','库存现金' union all select '现金','存现金' union all select '金','现金' go --2005 ;with cte as ( select convert(varchar(100),tname)tname,ttype from tb where tname = '金' union all select convert(varchar(100),a.tname+'-'+b.tname),a.ttype from tb a inner join cte b on a.tname = b.ttype ) select tname from cte a where not exists(select 1 from cte where len(tname)>len(a.tname)) /* tname ---------------------------------------------------------------------------------------------------- 库存现金-存现金-现金-金(1 行受影响) */ --or: ;with cte as ( select convert(varchar(100),tname)tname,ttype from tb where tname = '金' union all select convert(varchar(100),b.tname+'-'+a.tname),a.ttype from tb a inner join cte b on a.tname = b.ttype ) select tname from cte a where not exists(select 1 from cte where len(tname)>len(a.tname))/* tname ---------------------------------------------------------------------------------------------------- 金-现金-存现金-库存现金(1 行受影响)*/ go drop table tb
;with AcHerat as
(
select 名称,分类 from tb where 名称 = 'ee'
union all
select a.名称,a.分类 from tb a join AcHerat b on a.名称 = b.分类
)select stuff((select '-'+名称 from AcHerat order by 名称 for xml path('')),1,1,'')
use weimeicreate table #tb(tname varchar(10), ttype varchar(10))
insert #tb
select 'tt','' union all
select 'aa','' union all
select 'bb','aa' union all
select 'cc','bb' union all
select 'dd','cc' union all
select 'ee','dd' union all
select 'mm','tt'select * from #tb
where tname<'ee'
--tname ttype
--aa
--bb aa
--cc bb
--dd cc
create table tb (名称 varchar(10),性别 varchar(10),二级分类 varchar(10))
insert into tb
select 'a1','男人',null union all
select 'a2','女人',null union all
select 'b1',null,'a1' union all
select 'b2',null,'a1' union all
select 'b3',null,'a2' union all
select 'b4',null,'a2' union all
select 'c1',null,'b1' union all
select 'c2',null,'b2' union all
select 'd1',null,'c1' union all
select 'd2',null,'c2'
go
--2000
create function f_getCode(@code varchar(20))
returns @re table(名称 varchar(10),[level] int)
as
begin
declare @l int
set @l = 1
insert @re select 名称,@l from tb where 性别 = @code
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.名称,@l
from @re t join tb a on t.名称 = a.二级分类
where t.[level]=@l-1
end
return
end
go ;with AcHerat as
(
select 名称,二级分类 from tb where 性别 = N'男人'
union all
select a.名称,a.二级分类 from tb a join AcHerat b on a.二级分类 = b.名称
)select *
from AcHeratselect *
from dbo.f_getCode(N'男人')drop function f_getCode
drop table tb
字符串合并的可以参考这些资料:
有表tb, 如下:
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
需要得到结果:
id values
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即, group by id, 求 value 的和(字符串相加)1. 旧的解决方法-- 1. 创建处理函数
CREATE FUNCTION dbo.f_str(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r = ''
SELECT @r = @r + ',' + value
FROM tb
WHERE id=@id
RETURN STUFF(@r, 1, 1, '')
END
GO
-- 调用函数SELECt id, values=dbo.f_str(id)
FROM tb
GROUP BY id-- 2. 新的解决方法
-- 示例数据
DECLARE @t TABLE(id int, value varchar(10))
INSERT @t SELECT 1, 'aa'
UNION ALL SELECT 1, 'bb'
UNION ALL SELECT 2, 'aaa'
UNION ALL SELECT 2, 'bbb'
UNION ALL SELECT 2, 'ccc'-- 查询处理
SELECT *
FROM(
SELECT DISTINCT
id
FROM @t
)A
OUTER APPLY(
SELECT
[values]= STUFF(REPLACE(REPLACE(
(
SELECT value FROM @t N
WHERE id = A.id
FOR XML AUTO
), '<N value="', ','), '"/>', ''), 1, 1, '')
)N/*--结果
id values
----------- ----------------
1 aa,bb
2 aaa,bbb,ccc
(2 行受影响)
--*/--各种字符串分函数--3.3.1 使用游标法进行字符串合并处理的示例。
--处理的数据
CREATE TABLE tb(col1 varchar(10),col2 int)
INSERT tb SELECT 'a',1
UNION ALL SELECT 'a',2
UNION ALL SELECT 'b',1
UNION ALL SELECT 'b',2
UNION ALL SELECT 'b',3--合并处理
--定义结果集表变量
DECLARE @t TABLE(col1 varchar(10),col2 varchar(100))--定义游标并进行合并处理
DECLARE tb CURSOR LOCAL
FOR
SELECT col1,col2 FROM tb ORDER BY col1,col2
DECLARE @col1_old varchar(10),@col1 varchar(10),@col2 int,@s varchar(100)
OPEN tb
FETCH tb INTO @col1,@col2
SELECT @col1_old=@col1,@s=''
WHILE @@FETCH_STATUS=0
BEGIN
IF @col1=@col1_old
SELECT @s=@s+','+CAST(@col2 as varchar)
ELSE
BEGIN
INSERT @t VALUES(@col1_old,STUFF(@s,1,1,''))
SELECT @s=','+CAST(@col2 as varchar),@col1_old=@col1
END
FETCH tb INTO @col1,@col2
END
INSERT @t VALUES(@col1_old,STUFF(@s,1,1,''))
CLOSE tb
DEALLOCATE tb
--显示结果并删除测试数据
SELECT * FROM @t
DROP TABLE tb
/*--结果
col1 col2
---------- -----------
a 1,2
b 1,2,3
--*/
GO
/*==============================================*/
--3.3.2 使用用户定义函数,配合SELECT处理完成字符串合并处理的示例
--处理的数据
CREATE TABLE tb(col1 varchar(10),col2 int)
INSERT tb SELECT 'a',1
UNION ALL SELECT 'a',2
UNION ALL SELECT 'b',1
UNION ALL SELECT 'b',2
UNION ALL SELECT 'b',3
GO--合并处理函数
CREATE FUNCTION dbo.f_str(@col1 varchar(10))
RETURNS varchar(100)
AS
BEGIN
DECLARE @re varchar(100)
SET @re=''
SELECT @re=@re+','+CAST(col2 as varchar)
FROM tb
WHERE col1=@col1
RETURN(STUFF(@re,1,1,''))
END
GO--调用函数
SELECT col1,col2=dbo.f_str(col1) FROM tb GROUP BY col1
--删除测试
DROP TABLE tb
DROP FUNCTION f_str
/*--结果
col1 col2
---------- -----------
a 1,2
b 1,2,3
--*/
GO
if OBJECT_ID(N'Tbl1',N'U') is not null drop table Tbl1
go
create table Tbl1
(
名称 varchar(10) not null,
分类 varchar(10)
)
goinsert into Tbl1
select 'tt','' union all
select 'aa','' union all
select 'bb','aa' union all
select 'cc','bb' union all
select 'dd','cc' union all
select 'ee','dd' union all
select 'mm','tt'
declare @a nvarchar(max);
-------cte递归
with cte as
(
SELECT 名称,分类 FROM Tbl1 where 名称 = 'ee'
UNION ALL
select a.名称,a.分类 FROM Tbl1 a inner join cte b on a.名称 = b.分类
)
select @a = case isnull(@a, '') when '' then '' else @a + '-' end + 名称 from
cte order by 名称
select @a
create table tb(tname varchar(10), ttype varchar(10))
insert tb
select 'tt','' union all
select 'aa','' union all
select 'bb','aa' union all
select 'cc','bb' union all
select 'dd','cc' union all
select 'ee','dd' union all
select 'mm','tt'
go--2005
;with AcHerat as
(
select tname,ttype from tb where tname = 'ee'
union all
select a.tname,a.ttype from tb a join AcHerat b on a.tname = b.ttype
)select stuff((select '-'+tname from AcHerat order by tname for xml path('')),1,1,'')drop table tb/**************----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
aa-bb-cc-dd-ee
create table tb(tname varchar(10), ttype varchar(10))
insert tb
select 'tt','' union all
select 'aa','' union all
select 'bb','aa' union all
select 'cc','bb' union all
select 'dd','cc' union all
select 'ee','dd' union all
select 'mm','tt'
go--2000
create function f_getCode(@code varchar(20))
returns @re table(tname varchar(10),ttype varchar(10),[level] int)
as
begin
declare @l int
set @l = 1
insert @re select tname,ttype,@l from tb where tname = @code
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.tname,a.ttype,@l
from @re t join tb a on t.ttype = a.tname
where t.[level]=@l-1
end
return
end
go declare @str varchar(4000)
select @str = isnull(@str+'-','')+tname
from dbo.f_getCode('ee')
order by [level] descselect @strdrop function f_getCode
drop table tb/***************----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
aa-bb-cc-dd-ee
/*
标题: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 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 行受影响)
*/
顶这个
;with AcHerat as
(
select tname,ttype from tb where tname = 'ee'
union all
select a.tname,a.ttype from tb a join AcHerat b on a.tname = b.ttype
)select stuff((select '-'+tname from AcHerat order by tname for xml path('')),1,1,'')
(
select tname,ttype from tb where tname = 'ee'
union all
select a.tname,a.ttype from tb a join AcHerat b on a.tname = b.ttype
)select stuff((select '-'+tname from AcHerat order by tname for xml path('')),1,1,'')
这个就可以了。
tt
aa
bb aa
cc bb
dd cc
ee dd
mm tt
无限分类...create table #tb(名称 varchar(10), 分类 varchar(10))
insert #tb
select 'tt','' union all
select 'aa','' union all
select 'bb','aa' union all
select 'cc','bb' union all
select 'dd','cc' union all
select 'ee','dd' union all
select 'mm','tt'with cte as
(select 名称,分类 from #tb where 名称='ee'
union all
select #tb.名称,#tb.分类 from #tb,cte where #tb.名称=cte.分类)
select stuff((select '-'+名称 from cte order by 名称 for xml path('')),1,1,'')/*
-----------------
aa-bb-cc-dd-ee(1 行受影响)
*/
insert tb
select 'tt','' union all
select 'aa','' union all
select 'bb','aa' union all
select 'cc','bb' union all
select 'dd','cc' union all
select 'ee','dd' union all
select 'mm','tt'create function f_str(@名称 varchar(20))
returns @table table(名称 varchar(10),分类 varchar(10),[level] int)
as
begin
declare @i int
set @i = 1
insert @table select 名称,分类,@i from tb where 名称 = @名称
while @@rowcount>0
begin
set @i=@i+1
insert @table select a.名称,a.分类,@i
from @table t join tb a on t.分类 = a.名称
where t.[level]=@i-1
end
return
end
go declare @sql varchar(8000)
select @sql = isnull(@sql+'-','')+名称 from dbo.f_str('ee') where 名称<>'ee'
order by [level] descselect @sql/*
------------
aa-bb-cc-dd
*/
insert tb
select '库存现金','' union all
select '存现金','库存现金' union all
select '现金','存现金' union all
select '金','现金'
go
--2005
;with cte as
(
select convert(varchar(100),tname)tname,ttype from tb where tname = '金'
union all
select convert(varchar(100),a.tname+'-'+b.tname),a.ttype from tb a inner join cte b on a.tname = b.ttype
)
select tname from cte a where not exists(select 1 from cte where len(tname)>len(a.tname))
/*
tname
----------------------------------------------------------------------------------------------------
库存现金-存现金-现金-金(1 行受影响)
*/
--or:
;with cte as
(
select convert(varchar(100),tname)tname,ttype from tb where tname = '金'
union all
select convert(varchar(100),b.tname+'-'+a.tname),a.ttype from tb a inner join cte b on a.tname = b.ttype
)
select tname from cte a where not exists(select 1 from cte where len(tname)>len(a.tname))/*
tname
----------------------------------------------------------------------------------------------------
金-现金-存现金-库存现金(1 行受影响)*/
go
drop table tb