破解Oracle函数”wmsys.wm_concat()”

  • Post author:
  • Post category:其他




自定义此函数是由于“wmsys.wm_concat”在10G中不稳定。现已写成自己定义的函数,功能与用法和“wmsys.wm_concat”函数一样。赶紧贴出了给大伙看看。



备注: 这是从开源网转载而来的源码,经过本人的多次验证和实际运用此函数可行。

create or replace TYPE zh_concat_im 
AUTHID CURRENT_USER AS OBJECT
(
  CURR_STR VARCHAR2(32767),
  STATIC FUNCTION ODCIAGGREGATEINITIALIZE(SCTX IN OUT zh_concat_im) RETURN NUMBER,
  MEMBER FUNCTION ODCIAGGREGATEITERATE(SELF IN OUT zh_concat_im, 
               P1 IN VARCHAR2) RETURN NUMBER,
  MEMBER FUNCTION ODCIAGGREGATETERMINATE(SELF IN zh_concat_im,
                                 RETURNVALUE OUT VARCHAR2,
                                 FLAGS IN NUMBER)
                     RETURN NUMBER,
  MEMBER FUNCTION ODCIAGGREGATEMERGE(SELF IN OUT zh_concat_im, 
                    SCTX2 IN  zh_concat_im) RETURN NUMBER
);
/

create or replace TYPE BODY zh_concat_im
IS
  STATIC FUNCTION ODCIAGGREGATEINITIALIZE(SCTX IN OUT zh_concat_im)
  RETURN NUMBER 
  IS
  BEGIN
    SCTX := zh_concat_im(NULL) ;
    RETURN ODCICONST.SUCCESS;
  END;
  MEMBER FUNCTION ODCIAGGREGATEITERATE(SELF IN OUT zh_concat_im,
          P1 IN VARCHAR2) 
  RETURN NUMBER 
  IS
  BEGIN
    IF(CURR_STR IS NOT NULL) THEN 
      CURR_STR := CURR_STR || ',' || P1;
    ELSE
      CURR_STR := P1;
    END IF;
    RETURN ODCICONST.SUCCESS;
  END;
  MEMBER FUNCTION ODCIAGGREGATETERMINATE(SELF IN zh_concat_im,
                                 RETURNVALUE OUT VARCHAR2,
                                 FLAGS IN NUMBER)
    RETURN NUMBER 
  IS
  BEGIN
    RETURNVALUE := CURR_STR ;
    RETURN ODCICONST.SUCCESS;
  END;
  MEMBER FUNCTION ODCIAGGREGATEMERGE(SELF IN OUT zh_concat_im, 
                                   SCTX2 IN zh_concat_im) 
  RETURN NUMBER 
  IS
  BEGIN
    IF(SCTX2.CURR_STR IS NOT NULL) THEN 
      SELF.CURR_STR := SELF.CURR_STR || ',' || SCTX2.CURR_STR ;
    END IF;
    RETURN ODCICONST.SUCCESS;
  END;
END;
/

---函数
create or replace FUNCTION zh_concat(P1 VARCHAR2) 
RETURN VARCHAR2 AGGREGATE USING zh_concat_im ;


用法:


SELECT  zh_concat(str) from dual;  —调用函数



备注:以上红色字体可以自定义合并的区分符号。



版权声明:本文为zhangbo936原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。