【问题标题】:Getting ORA-22922 (nonexistent LOB value) or no result at all with wm_concat()使用 wm_concat() 获取 ORA-22922(不存在的 LOB 值)或根本没有结果
【发布时间】:2016-09-05 07:38:28
【问题描述】:

(使用 Oracle 11.2)

我有一个相当复杂的 SQL,比如

wm_concat( distinct abc )

预计会返回一些varchar2(4000) 兼容的结果

它会导致 ORA-00932: inconsistent datatypes 在我的选择中用于某些 coalesce( some_varchar_col, wm_concat( ... ) )


所以我尝试通过两种不同的方法投射它:

dbms_lob.substr( ..., 4000 )  -- L) tried even with 3000 in case of "unicode byte blow-up"
cast( ... as varchar2(4000))  -- C) tried even with 3000 in case of "unicode byte blow-up"

(在视图中使用,但玩弄它表明,它与视图无关)

根据列和其他运算符,我得到 N) 无结果或 O) ORA-22922

select * from view_with_above_included where rownum <= 100
  • N) 我的 Eclipse Data Explorer JDBC 连接返回没有任何结果(没有没有结果的列,没有(0 rows effected),只有查询时间统计信息)。 (这可能是一个内部异常没有被这样对待?)

  • O)

    ORA-22922: nonexistent LOB value
    ORA-06512: in "SYS.DBMS_LOB", line 1092
    ORA-06512: in line 1
    

奇怪的是,以下测试查询有效:

-- rownum <= 100 would already cause the above problems
select * from view_with_above_included where rownum <= 10

select * from view_with_above_included

但查看实际聚合数据并不会显示长度超过 1000 个字符的聚合数据。

【问题讨论】:

标签: oracle11g casting wm-concat


【解决方案1】:

幸运的是,它适用于自 11.2 以来提供的 listagg( ... ) 函数(我们已经在运行),因此我们无需进一步调查:

listagg( abc, ',' ) within group ( order by abc )

(应该知道,wm_concat(...) 是一些内部和官方不支持的功能。)


a rather nice solution(因为它没有那么臃肿)实现distinct 功能是通过自引用正则表达式功能,这在许多情况下应该可以工作:

regexp_replace( 
  listagg( abc, ',' ) within group ( order by abc )
, '(^|,)(.+)(,\2)+', '\1\2' )

(也许/希望我们将来会看到一些有效的 listagg( distinct abc ) 功能,这将像 wm_concat 语法一样非常简洁和酷。例如,这在 Postgres 的 string_agg( distinct abc ) 很长时间以来都没有问题sup>1)

-- 1: postgres sql example:
select string_agg( distinct x, ',' ) from unnest('{a,b,a}'::text[]) as x`

如果列表超过 4000 个字符,则有一个不能再使用listagg(再次ORA-22922)。 但幸运的是,我们可以在这里使用xmlagg 函数(如here 所述)。 如果您想在此处在 4000 个字符截断的结果上实现distinct,您可以(1) 标记的行注释掉

-- in smallercase everything that could/should be special for your query
-- comment in (1) to realize a distinct on a 4000 chars truncated result
WITH cfg AS ( 
  SELECT 
    ','                  AS list_delim,
    '([^,]+)(,\1)*(,|$)' AS list_dist_match,  -- regexp match for distinct functionality
    '\1\3'               AS LIST_DIST_REPL  -- regexp replace for distinct functionality
  FROM DUAL
)
SELECT
  --REGEXP_REPLACE( DBMS_LOB.SUBSTR(             -- (1)
  RTRIM( XMLAGG( XMLELEMENT( E, mycol, listdelim ).EXTRACT('//text()') 
  ORDER BY mycol ).GetClobVal(), LIST_DELIM ) 
  --, 4000 ), LIST_DIST_MATCH, LIST_DIST_REPL )  -- (1)
  AS mylist
FROM mytab, CFG

【讨论】:

  • distinct 但是必须区别对待,例如:stackoverflow.com/a/11511203/1915920
  • wm_concat 在 Oracle11g 中无论如何都不支持。它在 Oracle 10g 之前可用。
  • @Raj_Te: 错误...在 Oracle 12c 中删除(例如在 11.2 中工作),可用,但在存在的地方不受支持。
  • 我在11.2上使用wm_concat也遇到了错误,用listagg解决,子查询处理不同的值。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-23
  • 2016-02-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多