gpt4 book ai didi

sql - 如何处理 listagg() 函数中的异常

转载 作者:行者123 更新时间:2023-12-04 20:56:42 26 4
gpt4 key购买 nike

我正在我的脚本中使用 listagg

listagg(' |' || aktiv.AKTIVITÄT_NR || ' |' || aktiv.AKTIVITÄT_KÜRZEL  || ' |' || aktiv.AKTIVITÄT_BESCHREIBUNG || ' |' || aktiv.AKTIVITÄT_ERWARTETES_ERGEBNIS|| ' |' ||  CHR(10)) within group (order by aktiv.AKTIVITÄT_NR)) as activity 

当 listagg 超过 4000 字节时,所有脚本都会失败。我如何处理异常并为此记录插入 e.x. NULL 并转到下一条记录。

最佳答案

抱歉,我用我自己的例子来展示这个想法,因为我根本没有德语布局,也没有你的表格:

with src as (/* overflow */
select 1 id, level lv
from dual
connect by level <= 10000
union all
/* fitting */
select 2, level lv
from dual
connect by level <= 10
union all
select 3, level lv
from dual
connect by level <= 5)
select listagg(case when length_ <= 4000 then lv end,',') within group (order by lv)
from (select id,lv,sum(length(lv) + 1) over (partition by id) - 1 length_ from src)
group by id

想法:

  1. src 是你的表,id 是你分组的值,level 是你的值
  2. 这个子查询 select id,lv,sum(length(lv) + 1) over (partition by id) - 1 length_ from src收集 listagg 的 future 长度结果,+ 1为定界符制作',' , - 1为最后一个未使用的分隔符完成
  3. 表达式 listagg(case when length_ <= 4000 then lv end,',') within group (order by lv)检查长度是否小于允许值(4000),如果溢出则返回 null

希望这能解决您的问题。

关于sql - 如何处理 listagg() 函数中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20704081/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com