将字段值连接到 SQL Server 中的字符串

2021-12-09 00:00:00 csv sql concatenation sql-server

我需要一个与 SQL Server 中的 Oracle WM_CONCAT 类似的函数,它返回一个逗号分隔的列表,其中包含您将其作为参数传递的任何字段.例如,在 Oracle 中,

I need a similar function to Oracle WM_CONCAT in SQL Server, which returns a comma separated list of whatever field you pass it as argument. For example, in Oracle,

select WM_CONCAT(first_name) from employee where state='CA' 

返回约翰、吉姆、鲍勃".

returns "John, Jim, Bob".

如何在 SQL Server 中执行此操作?

How can I do this in SQL Server?

谢谢

推荐答案

在SQL Server 2017中增加了STRING_AGG函数

In SQL Server 2017 STRING_AGG function has been added

SELECT t.name as TableName
      ,STRING_AGG(c.name, ';') AS FieldList
  FROM sys.tables t
  JOIN sys.columns c 
    ON t.object_id = c.object_id
  GROUP BY t.name;

相关文章