SQL Server 2000 中的内爆类型函数? [英] Implode type function in SQL Server 2000?
问题描述
是否有适用于 SQL Server 的内爆类型函数?
Is there an Implode type function for SQL Server?
我有一个列表(在 SQL 服务器表中):
What I have is a list (in a SQL server table):
Apple
Orange
Pear
Blueberry
我希望他们以
Apple, Orange, Pear, Blueberry
我希望空格和逗号是可配置的,但如果不是,我可以随时替换它...
I hope for the space and comma to be configurable but I can always replace it if it isn't...
快速帮助将不胜感激!
推荐答案
SO 上已经有一些与此相关的问题(搜索 PIVOT 或 UNPIVOT 或 GROUP_CONCAT),但是 SQL Server 的一个简单解决方案(使用带有变量的技巧)串联)用于您陈述的问题:
There are some questions related to this already on SO (search for PIVOT or UNPIVOT or GROUP_CONCAT), but a simple solution for SQL Server (using a trick with variable concatenation) for your stated problem:
DECLARE @str AS varchar(MAX)
DECLARE @separator AS varchar(50)
SET @separator = ', ' -- Here's your configurable version
SELECT @str = COALESCE(@str + @separator, '') + <column_name>
FROM <table_name>
ORDER BY <sort_order>
当然,如果每行都需要这样做,您可以使用 UDF 或 非常酷的 FOR XML 技巧
Of course, if this is needed on a per-row basis, you can use UDFs or the really cool FOR XML trick
这篇关于SQL Server 2000 中的内爆类型函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!