可能重复:
SQL Server: Can I Comma Delimit Multiple Rows Into One Column?
我在数据源中有一堆值,我试图输出为一行:
SELECT
'|| Id == "' + cast(ID as varchar(100)) + '"'
FROM
dbo.CAItem
WHERE
isactive = 1
这是正确的数据,但是我想将它作为单行而不是每个结果的单独行输出,这样我的返回就像这样一行:
|| Id == "4431" || Id == "4436"
答案 0 :(得分:1)
SQL Server中没有GROUP_CONCAT()
函数(but please vote and comment here)。
与此同时,你可以这样做:
DECLARE @a TABLE(ID INT);
INSERT @a SELECT 4431 UNION ALL SELECT 4436;
SELECT *
FROM (SELECT '|| Id == "' + CONVERT(VARCHAR(100), ID) + '"'
FROM @a -- WHERE Isative = 1
FOR XML PATH('')) AS x(n);