将列值连接成逗号分隔的列表 [英] Concatenating Column Values into a Comma-Separated List

查看:17
本文介绍了将列值连接成逗号分隔的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

格式化输出的 TSQL 语法是什么,以便列值显示为字符串,以逗号分隔.

What is the TSQL syntax to format my output so that the column values appear as a string, seperated by commas.

例如,我的表 CARS 有以下内容:

Example, my table CARS has the following:

CarID    CarName  
----------------
    1    Porsche  
    2    Mercedes  
    3    Ferrari  

如何将汽车名称设为:Porsche、Mercedes、Ferrari

推荐答案

例如,您可以使用 coalesce 做一个快捷方式来连接表中记录中的一系列字符串.

You can do a shortcut using coalesce to concatenate a series of strings from a record in a table, for example.

declare @aa varchar (200)
set @aa = ''

select @aa = 
    case when @aa = ''
    then CarName
    else @aa + coalesce(',' + CarName, '')
    end
  from Cars

print @aa

这篇关于将列值连接成逗号分隔的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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