xp

导航

数据库行列转换

Posted on 2007-10-22 15:22  xp  阅读(434)  评论(0编辑  收藏  举报

数据表:

index       content       num
1           hello         1
1           mr            2
1           king          3
2           I             1
2           am            2
2           best          3
2           the           4


写出sql语句,得到一下查询结果:

查询结果:

index       content      
1           hello mr king
2           I am the best


alter function dbo.fun_content
(@id int)
RETURNS VARCHAR(32) AS
begin
declare @str varchar(500)
set @str = ''
select @str = @str + ' ' + content from test2 where id=@id order by num
return @str
end
go

select id,dbo.fun_content(t.id) from test2 t group by id