在单独的行上打印SQL结果

时间:2014-04-02 18:30:57

标签: sql sql-server

请参阅下面的SQL语句:

create table dbo.test(id int identity, Name varchar(10))
 insert into test values ('ian')
 insert into test values ('andy')
 insert into test values ('vicky')

SELECT TOP 1
       STUFF((SELECT ',' + Name AS [text()]
                FROM dbo.Test 
               ORDER BY id
                 FOR XML PATH('')), 1, 0, '' ) Concatenated
 FROM TEST

按预期返回单个字符串值:,ian,andy,vicky。如果我在网页上打印结果,它会打印:ian,andy,vicky如预期的那样。它有什么方法可以打印:

伊恩
安迪
维基

即。分开的线路?

2 个答案:

答案 0 :(得分:3)

SELECT TOP 1
       STUFF((SELECT '<BR/>' + Name AS [text()]
                FROM dbo.Test 
               ORDER BY id
                 FOR XML PATH('')), 1, 5, '' ) Concatenated
                 -- could be this if depending on any xxs settings on sql server
                 -- FOR XML PATH('')), 1, 11, '' ) Concatenated


 FROM TEST

答案 1 :(得分:1)

你的意思是?

SELECT NAME FROM TEST