我想在SQL Server中创建一个表值函数,我想以逗号分隔的值返回数据。例如表: tblID | Value---+------- 1 | 100 1 | 200 1 | 300 1 | 400 现在,当我使用函数执行查询时 Func1(value)SELECT Func1(Value) FROM tbl WHERE ID = 1我想要的输出是: 100,200,300,400
1 回答
HUWWW
TA贡献1874条经验 获得超12个赞
测试数据
DECLARE @Table1 TABLE(ID INT, Value INT)
INSERT INTO @Table1 VALUES (1,100),(1,200),(1,300),(1,400)
询问
SELECT ID
,STUFF((SELECT ', ' + CAST(Value AS VARCHAR(10)) [text()]
FROM @Table1
WHERE ID = t.ID
FOR XML PATH(''), TYPE)
.value('.','NVARCHAR(MAX)'),1,2,' ') List_Output
FROM @Table1 t
GROUP BY ID
结果集
╔════╦═════════════════════╗
║ ID ║ List_Output ║
╠════╬═════════════════════╣
║ 1 ║ 100, 200, 300, 400 ║
╚════╩═════════════════════╝
- 1 回答
- 0 关注
- 429 浏览
添加回答
举报
0/150
提交
取消