0
我有典型的表:插入多個值到一個單元格使用SQL 2005
LSRNbr BatchNbr
111 1212
111 1414
和查詢應該返回:
LSRNbr BatchNbr
111 1212, 1414
我瀏覽了一個解決方案,這一點,我發現這兩個:
解決方案1:
;WITH C AS
(
SELECT LSRNbr, BatchNbr FROM tblDTS_LSRBatch
)
SELECT Distinct LSRNbr,
STUFF((SELECT ';' + BatchNbr FROM tblDTS_LSRBatch WHERE LSRNbr = c.LSRNbr FOR XML PATH('')),1,1,'')
FROM C
錯誤:
Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near ';'.
Msg 170, Level 15, State 1, Line 7
Line 7: Incorrect syntax near 'XML'.
解決方案2:
SELECT
[LSRNbr], REPLACE(RTRIM((SELECT [BatchNbr] + ' ' FROM tblDTS_LSRBatch WHERE (LSRNbr = Results.LSRNbr) FOR XML PATH (''))),' ',', ') AS NameValues
FROM tblDTS_LSRBatch Results
GROUP BY LSRNbr
錯誤:
Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near 'XML'.
但他們沒有工作對我來說,看到上面的錯誤,請。
這裏有什麼問題?
我正在使用Microsoft SQL Server 2005
完美,我感謝您的幫助:) – Somebody 2012-04-17 14:19:00
是與我的版本的SQL Server相關的東西,因爲我在SQL 2008中做了同樣的事情,它的工作原理是:( – Somebody 2012-04-17 14:46:22
哦......這一直髮生在我身上。對不起如果我誤導了你 – dfb 2012-04-17 15:11:04