开发者

Combine fields in SQL Server

开发者 https://www.devze.com 2023-01-06 00:23 出处:网络
How to combine开发者_如何学Go rows in SQL Server 2000Have you tried using FOR XML RAW in SQL Server 2000?You can create a user defined function to perform the string concatenation for each ID value.

How to combine开发者_如何学Go rows in SQL Server 2000


Have you tried using FOR XML RAW in SQL Server 2000?


You can create a user defined function to perform the string concatenation for each ID value.

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

精彩评论

暂无评论...
验证码 换一张
取 消