2010-07-06 39 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