Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7939141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:00:41+00:00 2026-06-03T23:00:41+00:00

i wrote a simple aggregate function that should work on columns with csv text

  • 0

i wrote a simple aggregate function that should work on columns with csv text in them by aggregating distinct values in a resulting csv string. the functions seems to work all the way to the end when it craps out with the ORA-06502 error right when it should be returning the result.

here is the code:

type def:

create or replace type array_union_typ as object (

union_agg nvarchar2(1000),

static function ODCIAggregateInitialize(sctx  in out array_union_typ)
                return number,

member function ODCIAggregateIterate   (self  in out array_union_typ,
                                        value in nvarchar2)
                return number,

member function ODCIAggregateTerminate (self         in array_union_typ,
                                        return_value out nvarchar2,
                                        flags        in number)
                return number,

member function ODCIAggregateMerge(self in out array_union_typ,
                                   ctx2 in array_union_typ)
                return number,

static function agg_union(arg1 in nvarchar2,
                          arg2 in nvarchar2)
                return nvarchar2
);

the body:

create or replace type body array_union_typ  is

static function ODCIAggregateInitialize(sctx in out array_union_typ
                                     ) return number is
begin
    sctx := array_union_typ(null);
    return ODCIConst.Success;
end;

member function ODCIAggregateIterate(self in out array_union_typ,
                                     value in nvarchar2
                                     ) return number is
begin
    union_agg := array_union_typ.agg_union(union_agg, value);
    return ODCIConst.Success;
end;

member function ODCIAggregateTerminate(self in array_union_typ,
                                       return_value out nvarchar2,
                                       flags in number
                                       ) return number is
begin
    dbms_output.put_line('result: '''|| union_agg || ''', length:' || length(union_agg)); --this still prints
    return_value := self.union_agg; -- <-- this is where the error is indicated
    --return_value := 'x'; -- returning this still gives the error.
    return ODCIConst.Success;
end;

member function ODCIAggregateMerge(self in out array_union_typ,
                                   ctx2 in array_union_typ
                                   ) return number is
begin
    union_agg := array_union_typ.agg_union(union_agg, ctx2.union_agg);
    return ODCIConst.Success;
end;

static function agg_union(arg1 in nvarchar2,
                          arg2 in nvarchar2)
                return nvarchar2 is
    result nvarchar2(1000);
    orig nvarchar2(1000);
begin
    dbms_output.enable;
    orig := replace(arg1||','||arg2, chr(0));

    FOR rec IN (SELECT DISTINCT(regexp_substr(orig, '[^,]+', 1, level)) AS a
           FROM dual CONNECT BY regexp_substr(orig, '[^,]+', 1, level) IS NOT NULL ORDER BY a) LOOP
      IF result IS NOT NULL THEN
        result := result || ',' || rec.a;
      ELSE
        result := rec.a;
      END IF;
    END LOOP;
    --dbms_output.put_line('endwith: ''' || result || '''');
    RETURN substr(result,1,1000);
end;
end;

here is a test table and data:

SQL> desc uniontest
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 I                                                  NVARCHAR2(50)

SQL> select * from uniontest;

I
--------------------------------------------------
a
a
b,c
b,d,e

and finally, this is what happens if i try to use the aggregate function:

SQL> select array_union(i) from uniontest;
select array_union(i) from uniontest
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at "M35456.ARRAY_UNION_TYP", line 25


result: 'a,b,c,d,e', length:9

if i simply pass a single character string like ‘x’ in the offending line, i still get the same error. only on a null result does it go away. i am stumped and out of ideas.

thanks for any help.

btw, if anyone has any idea why i get added \0 characters i my agg_union function parameters, i am dying to know about that, too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:00:43+00:00Added an answer on June 3, 2026 at 11:00 pm

    The ODCIaggregate methods don’t seem to be playing nicely with nvarchar2; you don’t appear to be doing anything wrong since it works fine if those are all changed to varchar2.

    If you are on 11gR2 and thus have the listagg function available – but currently aren’t using that because you need to figure out the distinct values – you can combine it with your existing regexp_substr function, so you don’t need your own type/function:

    SELECT REPLACE(LISTAGG(a, ',') WITHIN GROUP (ORDER BY a), chr(0), '')
    FROM (
        SELECT DISTINCT(regexp_substr(i, '[^,]+', 1, level)) AS a
        FROM uniontest CONNECT BY regexp_substr(i, '[^,]+', 1, level) IS NOT NULL
        ORDER BY a
    );
    

    … which with your data gives:

    REPLACE(LISTAGG(A,',')WITHINGROUP(ORDERBYA),CHR(0),'')
    ------------------------------------------------------
    a,b,c,d,e
    

    In earlier versions you can use SELECT REPLACE(WM_CONCAT(a), chr(0), '') instead.

    (The chr(0) here and the \0 in your question seem to be related to nvarchar2, but someone else will need to chip in on the details…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a simple javascript function to display a progressbar with the help of
I wrote a simple Sinatra app that generate an image using rmagick from some
i wrote a simple function in controller public string LinkProjectSquareFilter(int squareId) { return squareId.ToString();
I wrote a simple script that is intended to do hierarchical clustering on a
I wrote simple application that using the cell phone camera. When i trying to
I wrote a User Defined Function that gets a UtcTimeStamp and a windowstimezoneid as
I wrote simple NMEA parser and I'm reading latitude and longitude from GPS. Values
I wrote simple update/insert statements that are returning a syntax error, what am I
I wrote simple application that send some data to my server. Somehow the data
I wrote simple function which handles fetching of the url: def tender_page_get url, agent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.