Oracle自定义聚合函数 拼接超长字符串

Oracle拼接字符串是由长度限制的,可以自定义聚合函数来解决这个问题

create or replace TYPE T_LINK AS OBJECT ( 
   STR VARCHAR2(32767), 
   STATIC FUNCTION ODCIAGGREGATEINITIALIZE(SCTX IN OUT T_LINK) RETURN NUMBER, 
   MEMBER FUNCTION ODCIAGGREGATEITERATE(SELF IN OUT T_LINK, VALUE IN VARCHAR2) RETURN NUMBER, 
   MEMBER FUNCTION ODCIAGGREGATETERMINATE(SELF IN T_LINK, RETURNVALUE OUT CLOB, FLAGS IN NUMBER) RETURN NUMBER, 
   MEMBER FUNCTION ODCIAGGREGATEMERGE(SELF IN OUT T_LINK, CTX2 IN T_LINK) RETURN NUMBER 
   );


CREATE OR REPLACE TYPE BODY T_LINK IS
    STATIC FUNCTION ODCIAGGREGATEINITIALIZE(SCTX IN OUT T_LINK) RETURN NUMBER IS
    BEGIN
    SCTX := T_LINK(NULL);
    RETURN ODCICONST.SUCCESS;
    END;
    
    MEMBER FUNCTION ODCIAGGREGATEITERATE(SELF IN OUT T_LINK, VALUE IN VARCHAR2) RETURN NUMBER IS
    BEGIN
    SELF.STR := SELF.STR || VALUE || ',';
    RETURN ODCICONST.SUCCESS;
    END;
    
    MEMBER FUNCTION ODCIAGGREGATETERMINATE(SELF IN T_LINK, RETURNVALUE OUT CLOB, FLAGS IN NUMBER) RETURN NUMBER IS
    BEGIN
    RETURNVALUE := SUBSTR(SELF.STR, 1, LENGTH(SELF.STR) - 1);
    RETURN ODCICONST.SUCCESS;
    END;
    
    MEMBER FUNCTION ODCIAGGREGATEMERGE(SELF IN OUT T_LINK, CTX2 IN T_LINK) RETURN NUMBER IS
    BEGIN
    NULL;
    RETURN ODCICONST.SUCCESS;
    END;
    END;
/
   

create or replace FUNCTION F_LINK(P_STR VARCHAR2) RETURN CLOB 
    AGGREGATE USING T_LINK; 

--调用
select F_LINK('字符串') from dual;

你可能感兴趣的:(Oracle,oracle)