使用 COALESCE 函数使值以逗号分隔 [英] Using COALESCE function to make values separated with commas

查看:26
本文介绍了使用 COALESCE 函数使值以逗号分隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表 (EMP) 我知道使用 COALESCE 函数我们可以通过这种方式获取任何列的值

I have a table (EMP) I know that using the COALESCE function we can get the values of any column in this way

23,23,45,34

SELECT OfferID  FROM Emp where EmpID= 23

但我没有得到实现这一点的语法

but I am not getting the syntax to achieve this

解决此问题的任何帮助都会很棒.

Any help would be great, to resolve this issue.

推荐答案

DECLARE @List VARCHAR(8000)

SELECT @List = COALESCE(@List + ',', '') + CAST(OfferID AS VARCHAR)
FROM   Emp
WHERE  EmpID = 23

SELECT @List 

这种聚合串联的方法是不保证上班.如果您至少使用 SQL Server 2005 XML PATH 或 CLR 聚合是首选.

This approach to aggregate concatenation is not guaranteed to work. If you are on at least SQL Server 2005 XML PATH or CLR aggregates are preferred.

关于这个主题的权威文章是 在 Transact-SQL 中连接行值

The definitive article on the subject is Concatenating Row Values in Transact-SQL

这篇关于使用 COALESCE 函数使值以逗号分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆