自定义聚合函数T_LINK(解决ORA-06502错误)

自定义聚合函数:
将group by之后某列的多行数值聚合成一个字符串,用逗号隔开(当聚合之后字符串长度超过4000之后,将超过的字符用’…’来代替,解决了ORA-06502这一错误。)

--创建function
CREATE OR REPLACE FUNCTION "F_LINK" (P_STR VARCHAR2) RETURN VARCHAR2
AGGREGATE USING T_LINK;

--创建type
CREATE OR REPLACE TYPE "T_LINK"                                                                                                                                                   AS OBJECT (
STR VARCHAR2(30000),
STATIC FUNCTION LINKINITIALIZE(SCTX IN OUT T_LINK) RETURN NUMBER,
MEMBER FUNCTION LINKITERATE(SELF IN OUT T_LINK, VALUE IN VARCHAR2) RETURN NUMBER,
MEMBER FUNCTION LINKTERMINATE(SELF IN T_LINK, RETURNVALUE OUT VARCHAR2, FLAGS IN NUMBER) RETURN NUMBER,
MEMBER FUNCTION LINKMERGE(SELF IN OUT T_LINK, CTX2 IN T_LINK) RETURN NUMBER
)

--创建TypeBody 
CREATE OR REPLACE TYPE BODY T_LINK IS

STATIC FUNCTION LINKINITIALIZE(SCTX IN OUT T_LINK) RETURN NUMBER IS
BEGIN
SCTX := T_LINK(NULL);
RETURN ODCICONST.SUCCESS;
END;

MEMBER FUNCTION LINKITERATE(SELF IN OUT T_LINK, VALUE IN VARCHAR2) RETURN NUMBER IS
BEGIN
SELF.STR := SELF.STR ||','|| VALUE;
RETURN ODCICONST.SUCCESS;
END;

MEMBER FUNCTION LINKTERMINATE(SELF IN T_LINK, RETURNVALUE OUT VARCHAR2, FLAGS IN NUMBER) RETURN NUMBER IS
BEGIN
IF LENGTHB(SELF.STR) > 4000 THEN
RETURNVALUE := SUBSTR(SELF.STR,0,INSTR(SUBSTR(SELF.STR,0,3996),',',-1)) || '...';
ELSE
RETURNVALUE := LTRIM(SELF.STR,',');
END IF;
RETURN ODCICONST.SUCCESS;
END;

MEMBER FUNCTION LINKMERGE(SELF IN OUT T_LINK, CTX2 IN T_LINK) RETURN NUMBER IS
BEGIN
NULL;
RETURN ODCICONST.SUCCESS;
END;
END;

你可能感兴趣的:(Oracle)