这是我在 sql server 2016 中的代码
insert into @entdef_queries(entitydefid,squery)
select A.entitydefid
,
(
select String_agg(cols,ioperator)
from
(
Select case when lower(b.metricdatatype) like 'string%' or lower(b.metricdatatype) like '%char%' or lower(b.metricdatatype) ='bit' or lower(b.metricdatatype) like 'date%' then
' lower("'+ b.metricname +'") ' + b.metriccondition +' '''+ b.value1 +''' '
when lower(b.metricdatatype) not like 'string%' and lower(b.metricdatatype) like '%char%' and lower(b.metricdatatype) !='bit' and lower(b.metricdatatype) not like 'date%' then
case when lower(b.metriccondition)='between' then ' "'+ b.metricname +'"' + b.metriccondition +' '+ b.value1 +' and ' + b.value2 + ' '
else ' "'+ b.metricname +'"' + b.metriccondition +' '+ b.value1 + ' ' end
end cols
, ( select distinct operators from @entdef_data C where A.entitydefid=C.entitydefid) ioperator
from
@entdef_data B
where A.entitydefid=b.entitydefid
)inp
)
from
@entdef_data A
group by A.entitydefid;
当我尝试执行以下代码时..它抛出错误String_agg
不是内置函数。
正如 Gordon Linoff 提到的,此功能在 SQL Server 2016 中不可用。
The for xml
方法是要使用的。
有一个稍微faster https://sqlperformance.com/2014/08/t-sql-queries/sql-server-grouped-concatenation从 SQL Server 2005 开始可以使用的替代方案:SQLCLR GROUP_CONCAT
。
其用法与本机 STRING_AGG 非常相似。但因为它是自定义的 CLR 聚合,所以它引入了自身的风险:启用 CLR、可能的内存泄漏等。
文档:https://orlando-colamatteo.github.io/ms-sql-server-group-concat-sqlclr/ https://orlando-colamatteo.github.io/ms-sql-server-group-concat-sqlclr/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)