我有典型的桌子:
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