Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Listag函数和ORA-01489:字符串连接的结果太长_Java_Oracle_Jdbc_Oracle10g_Oracle11g - Fatal编程技术网

Java Listag函数和ORA-01489:字符串连接的结果太长

Java Listag函数和ORA-01489:字符串连接的结果太长,java,oracle,jdbc,oracle10g,oracle11g,Java,Oracle,Jdbc,Oracle10g,Oracle11g,当我运行以下查询时: Select tm.product_id, listagg(tm.book_id || '(' || tm.score || ')',',') within group (order by tm.product_id) as matches from tl_product_match tm where tm.book_id is not null group by tm.product_id Oracle返回以下错误: ORA-0148

当我运行以下查询时:

 Select
  tm.product_id,
  listagg(tm.book_id || '(' || tm.score || ')',',')
    within group (order by tm.product_id) as matches
from
  tl_product_match tm 
where
  tm.book_id is not null 
group by
  tm.product_id
Oracle返回以下错误:

 ORA-01489: result of string concatenation is too long
我知道它失败的原因是Listag函数试图将一个大于4000个字符的值连接起来,而这是不受支持的

我已经看到了这里描述的另一个示例,但是它们都需要使用函数或过程

有没有一种解决方案是纯SQL的,而不必调用函数或存储过程,并且能够使用标准JDBC读取值

我遇到的另一个困难是,我看到的大多数字符串聚合示例都显示了如何按原样读取值的示例。在我关于的示例中,我首先修改值(即,我聚合两列)

我已经看到了这里描述的另一个示例,但是它们都需要使用函数或过程


不,他们没有。向下滚动,您将看到几个不需要pl/sql的选项。

您可以使用返回CLOB的xml函数来执行此操作。JDBC对此应该没问题

select tm.product_id, 
       rtrim(extract(xmlagg(xmlelement(e, tm.book_id || '(' || tm.score || '),')), 
               '/E/text()').getclobval(), ',')
  from tl_product_match tm
 where tm.book_id is not null 
 group by tm.product_id;

例如:

为什么不使用嵌套表

set echo on;
set display on;
set linesize 200;

drop table testA;
create table testA
(
col1 number,
col2 varchar2(50)
);

drop table testB;
create table testB
(
col1 number,
col2 varchar2(50)
);

create or replace type t_vchar_tab as table of varchar2(50);

insert into testA values (1,'A');
insert into testA values (2,'B');

insert into testB values (1,'X');
insert into testB values (1,'Y');
insert into testB values (1,'Z');
commit;

-- select all related testB.col2 values in a nested table for each testA.col1 value
select a.col1, 
cast(multiset(select b.col2 from testB b where b.col1 = a.col1 order by b.col2) as t_vchar_tab) as testB_vals
from testA a;

-- test size > 4000
insert into testB
select 2 as col1, substr((object_name || object_type), 1, 50) as col2
from all_objects;
commit;

-- select all related testB.col2 values in a nested table for each testA.col1 value
select a.col1, 
cast(multiset(select b.col2 from testB b where b.col1 = a.col1 order by b.col2) as t_vchar_tab) as testB_vals
from testA a;

我不是java专家,但这已经有一段时间了,我相信java可以从嵌套表中提取值。而且,不需要在另一端标记某些分隔字符串。

我不清楚这些解决方案中的任何一个都支持返回
CLOB
,并且不需要创建任何PL/SQL对象。您认为该页面上的哪些选项符合这两个标准?我尝试了XML版本,但速度非常慢。它通常比纯sql慢吗?使用Listag的原始查询需要0.872秒,而XML版本需要4.461秒secs@ziggy它可能会慢一些,因为我们必须涉及xml,而且这将返回一个clob,这将有一些开销。另外,我假设您的计时是在Listag没有崩溃的同一个集合上:)请注意,
xmlagg()
支持
orderby
子句:
xmlagg(xmlement(e,…)orderby tm.product\u id)
。这是一个很棒的建议