2010-07-06 46 views

回答

1

您是否尝试过在SQL Server 2000中使用FOR XML RAW

+0

我会尝试..谢谢你先生 – learner 2010-07-06 04:46:36

+0

@learner:不知道你为什么编辑你的原始帖子来删除所有的细节,但我很想知道你如何在单个查询中使用XML RAW来实现输出您在原始问题中指定的格式。请发布您的解决方案。 – 2010-07-09 13:51:56

+0

对于XML原始不会工作..我试过。 – learner 2010-07-13 03:48:06

0

您可以创建用户定义的函数为每个ID值执行字符串连接。

create table t (id int,start varchar(100),finish varchar(100)) 
insert into t 
select 1,'Start_Main', '' union all 
select 1,'Start_Submain1', '' union all 
select 2,'Start_Main', '' union all 
select 2,'Start_Submain2', 'End_Submain2' union all 
select 2,'Start_Submain3', 'End_Submain3' union all 
select 2,'Start_Submain1', '' union all 
select 2,'Start_Submain4', 'End_Submain4' 
Select * from t 
go 

/* User Defined Function to perform string concatenation per ID */ 
create function udfStringConcat (@ID int) 
returns varchar(500) 
as 
begin 
    declare @x varchar(500) 
    set @x = '' 

    select @x = @x + t.start + ',' + case when t.finish <> '' then t.finish + ',' else t.finish end 
     from t 
     where t.id = @ID 

    select @x = @x + 'End_Submain1,End_Main' 

    return @x 
end 
go 

select id, dbo.udfStringConcat(id) 
    from t 
    group by id 
go 

drop function udfStringConcat 
drop table t 
go 
+0

这就是我不想做的事情。我想在单个查询中完成它(如在sql 2005中的xml path())。 – learner 2010-07-06 03:45:17