oracle函数 wm_concat 与 listagg

1. wm_concat

select t.type, to_char(wm_concat(t.id||'---'||t.seq)) as id_seq, to_char(wm_concat(t.seq)) as seqs
from (
    select 'A' type, 'A001' id,  1 seq from dual union all 
    select 'A' type, 'A002' id,  2 seq from dual union all 
    select 'B' type, 'B001' id,  1 seq from dual 
) t group by t.type;

查询结果

 

2.listagg

复制代码
SELECT 
  T.type,
  listagg(T.id ||'---'||T.seq, ',') WITHIN GROUP(ORDER BY T .seq) id_seq,
  listagg(T.seq, ',') WITHIN GROUP(ORDER BY T .seq desc) seqs
FROM (
    select 'A' type, 'A001' id,  1 seq from dual union all 
    select 'A' type, 'A002' id,  2 seq from dual union all 
    select 'B' type, 'B001' id,  1 seq from dual 
) t 
WHERE T.type in ('A', 'B') 
GROUP BY T.type;
复制代码

查询结果

 

3.wm_concat存在问题
a.该函数不是oracle公开的系统函数,它的用户是wmsys,而不是sys或者system,oracle很有可能在版本升级或者补丁的时候取消或者修改这个函数甚至用户,这种变化oracle是不会公开的。所有可能会由于这个变化而导致异常。
b.大量使用这个函数也会导致临时表空间爆满,这是因为在10.2.0.5中,使用wmsys.wm_concat返回的结果格式是CLOB,CLOB占用的临时表空间只有在连接释放后才会释放,部分通过连接池连接数据库的长连接很有可能导致CLOB占用临时表空间不断累积增大,会导致临时表空间爆满的故障。
c.如果是在程序中大量使用这个函数的话会引起enq:TT的锁,可能会导致某些对象被锁。
d.wm_concat在11g中使用需要用to_char()进行转换,否则会出现不兼容现象

 

4.建议
oracle11g后 推荐使用 listagg 函数,也可以参考wm_concat自己建立一个函数实现相同的行列转换功能

posted @   每天进步多一点  阅读(494)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
历史上的今天:
2022-06-29 JQuery_九大选择器
2022-06-29 jQuery 选择同时包含两个class的元素的实现方法
2021-06-29 开窗函数 SUM() OVER()
2018-06-29 C#操作Sqlite快速入门及相关工具收集
2017-06-29 用sp_lock诊断SQL Sever的死锁问题
2016-06-29 修改CKFinder上传路径
点击右上角即可分享
微信分享提示