SQL 查询 - 将结果连接到一个字符串中
我有一个包含以下代码的 sql 函数:
DECLARE @CodeNameString varchar(100)
SELECT CodeName FROM AccountCodes ORDER BY Sort
我需要将选择查询的所有结果连接到 CodeNameString 中。
显然,C# 代码中的 FOREACH 循环可以做到这一点,但如何在 SQL 中做到这一点呢?
I have a sql function that includes this code:
DECLARE @CodeNameString varchar(100)
SELECT CodeName FROM AccountCodes ORDER BY Sort
I need to concatenate all results from the select query into CodeNameString.
Obviously a FOREACH loop in C# code would do this, but how do I do it in SQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您使用的是 SQL Server 2005 或更高版本,则可以使用此
FOR XML PATH & STUFF
技巧:FOR XML PATH('')
基本上将字符串连接成一个长 XML 结果(例如,code1,code2,code3
等) .) 并且STUFF
在第一个字符处放置一个“无”字符,例如擦除“多余”的第一个逗号,以给出您可能正在寻找的结果。更新: 好的 - 我理解这些注释 - 如果数据库表中的文本已包含
<
、>
或等字符&
,那么我当前的解决方案实际上会将它们编码为<
、>
和 <代码>&。如果您对该 XML 编码有疑问 - 那么是的,您必须查看 @KM 提出的解决方案,它也适用于这些字符。我警告一句话:这种方法需要更多资源和处理密集型 - 只是让您知道。
If you're on SQL Server 2005 or up, you can use this
FOR XML PATH & STUFF
trick:The
FOR XML PATH('')
basically concatenates your strings together into one, long XML result (something like,code1,code2,code3
etc.) and theSTUFF
puts a "nothing" character at the first character, e.g. wipes out the "superfluous" first comma, to give you the result you're probably looking for.UPDATE: OK - I understand the comments - if your text in the database table already contains characters like
<
,>
or&
, then my current solution will in fact encode those into<
,>
, and&
.If you have a problem with that XML encoding - then yes, you must look at the solution proposed by @KM which works for those characters, too. One word of warning from me: this approach is a lot more resource and processing intensive - just so you know.
@AlexanderMP 的答案是正确的,但您也可以考虑使用
coalesce
处理空值:@AlexanderMP's answer is correct, but you can also consider handling nulls with
coalesce
:对于 SQL Server 2005 及更高版本,请使用 Coalesce 来处理
null 并且我正在使用 Cast 或 Convert 如果有
数值
-For SQL Server 2005 and above use Coalesce for
nulls
and I am using Cast or Convert if there arenumeric values
-来自 msdn 不要在 SELECT 语句中使用变量来连接值(即计算聚合值)。可能会出现意外的查询结果。这是因为 SELECT 列表中的所有表达式(包括赋值)不能保证对每个输出行精确执行一次
上面似乎说上面所做的串联是无效的,因为赋值可能执行的次数多于行数由选择返回
from msdn Do not use a variable in a SELECT statement to concatenate values (that is, to compute aggregate values). Unexpected query results may occur. This is because all expressions in the SELECT list (including assignments) are not guaranteed to be executed exactly once for each output row
The above seems to say that concatenation as done above is not valid as the assignment might be done more times than there are rows returned by the select
这是另一个现实生活中的示例,至少在 2008 年版本(及更高版本)中运行良好。
这是原始查询,它使用简单的
max()
来获取至少一个值:改进版本,主要改进是我们显示以逗号分隔的所有值:
请注意,我们已经解决了所有可能的问题我能想到的 NULL 案例问题,我们还修复了数值(字段排序)的错误。
Here is another real life example that works fine at least with 2008 release (and later).
This is the original query which uses simple
max()
to get at least one of the values:Improved version, where the main improvement is that we show all values comma separated:
Note that we have solved all possible
NULL
case issues that I can think of and also we fixed an error that we got for numeric values (field Sorting).