Listagg function and ORA-01489: result of string concatenation is too long

前端 未结 3 1753
萌比男神i
萌比男神i 2020-12-17 18:56

When i run the following query:

 Select
  tm.product_id,
  listagg(tm.book_id || \'(\' || tm.score || \')\',\',\')
    within group (order by tm.product_id)          


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-17 19:40

    Why not use nested tables?

    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;
    

    I'm no java expert, but this has been around for some time and I'm sure java can pull the values out of the nested table. And, no need to tokenize some delimited string on the other end.

提交回复
热议问题