我编写了一个简单的聚合函数,它应该通过聚合生成的 csv 字符串中的不同值来处理包含 csv 文本的列。当它应该返回结果时,当它出现 ORA-06502 错误时,这些函数似乎一直工作到最后。
这是代码:
类型定义:
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
);
身体:
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;
这是一个测试表和数据:
SQL> desc uniontest
Name Null? Type
----------------------------------------- -------- ----------------------------
I NVARCHAR2(50)
SQL> select * from uniontest;
I
--------------------------------------------------
a
a
b,c
b,d,e
最后,如果我尝试使用聚合函数会发生这种情况:
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
如果我只是在违规行中传递一个像“x”这样的字符串,我仍然会得到同样的错误。只有在空结果上它才会消失。我被难住了,没有想法。
谢谢你的帮助。
顺便说一句,如果有人知道为什么我在 agg_union 函数参数中添加了 \0 字符,我也很想知道这一点。