2013-04-08 106 views
0

我需要從一個查詢中獲取值作爲單行,用一個分隔符分隔,但選擇了我,但這有點棘手。如何獲得查詢結果中包含多行的結果,在一行中由一個分隔符分隔?

所以,我有:

select employer from employers 

我將返回:

(line1) Anne 
(line2) Sue 
(line3) Villy 
(line4) John 
(nth line) Marry 

我需要的結果是這樣的:

'Anne','Sue','Villy','John','Marry' 

或者至少是:

Anne^Sue^Villy^John^Marry 

任何提示? 謝謝,

回答

2
declare @s varchar(max) = '' 

select @s = case 
    when @s <> '' then 
     @s + ',''' + employer + '''' 
    else 
     @s + '''' + employer + '''' end 
from employers 

select @s 
+0

萬分感謝。從遊標中救了我!它像一個魅力。祝你有美好的一天! – BogdanM 2013-04-08 13:57:55

2
SELECT STUFF((SELECT DISTINCT ', ' + Employer AS [text()] 
FROM Employers 
FOR XML PATH ('')),1,1,'') 
相關問題