• oracle12 listagg 与 wm_concat行列转换


    11gr2和12C上已经摒弃了wm_concat函数,当时我们很多程序员在程序中确使用了该函数,导致程序出现错误,为了减轻程序员修改程序的工作量,只有通过手工创建个wm_concat函数,来临时解决该问题,但是注意,即使创建了该函数,在使用的过程中,也需要用to_char(wm_concat())方式,才能完全替代之前的应用。
    一.解锁wmsys用户

    alter user wmsys account unlock;

    二.创建包、包体和函数
    以wmsys用户登录数据库,执行下面的命令

    CREATE OR REPLACE TYPE WM_CONCAT_IMPL AS OBJECT  
    -- AUTHID CURRENT_USER AS OBJECT  
    (  
    CURR_STR VARCHAR2(32767),   
    STATIC FUNCTION ODCIAGGREGATEINITIALIZE(SCTX IN OUT WM_CONCAT_IMPL) RETURN NUMBER,  
    MEMBER FUNCTION ODCIAGGREGATEITERATE(SELF IN OUT WM_CONCAT_IMPL,  
    P1 IN VARCHAR2) RETURN NUMBER,  
    MEMBER FUNCTION ODCIAGGREGATETERMINATE(SELF IN WM_CONCAT_IMPL,  
    RETURNVALUE OUT VARCHAR2,  
    FLAGS IN NUMBER)  
    RETURN NUMBER,  
    MEMBER FUNCTION ODCIAGGREGATEMERGE(SELF IN OUT WM_CONCAT_IMPL,  
    SCTX2 IN WM_CONCAT_IMPL) RETURN NUMBER  
    );  
    /  
      
    --定义类型body:  
    CREATE OR REPLACE TYPE BODY WM_CONCAT_IMPL  
    IS  
    STATIC FUNCTION ODCIAGGREGATEINITIALIZE(SCTX IN OUT WM_CONCAT_IMPL)  
    RETURN NUMBER  
    IS  
    BEGIN  
    SCTX := WM_CONCAT_IMPL(NULL) ;  
    RETURN ODCICONST.SUCCESS;  
    END;  
    MEMBER FUNCTION ODCIAGGREGATEITERATE(SELF IN OUT WM_CONCAT_IMPL,  
    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 WM_CONCAT_IMPL,  
    RETURNVALUE OUT VARCHAR2,  
    FLAGS IN NUMBER)  
    RETURN NUMBER  
    IS  
    BEGIN  
    RETURNVALUE := CURR_STR ;  
    RETURN ODCICONST.SUCCESS;  
    END;  
    MEMBER FUNCTION ODCIAGGREGATEMERGE(SELF IN OUT WM_CONCAT_IMPL,  
    SCTX2 IN WM_CONCAT_IMPL)  
    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 wm_concat(P1 VARCHAR2)  
    RETURN VARCHAR2 AGGREGATE USING WM_CONCAT_IMPL ;  
    /

    三.创建同义词并授权

    create public synonym WM_CONCAT_IMPL for wmsys.WM_CONCAT_IMPL;
    create public synonym wm_concat for wmsys.wm_concat;
    grant execute on WM_CONCAT_IMPL to public;
    grant execute on wm_concat to public;

    或者使用listagg函数

    以下为类比方法

    select deptno, listagg(ename,',') within group(order by ename)
    from scott.emp 
    group by deptno
    
        DEPTNO PATH
    ---------- --------------------------------------------------------------------------------
     10 CLARK,KING,MILLER
     20 ADAMS,FORD,JONES,SCOTT,SMITH
     30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
    
    select deptno,wm_concat(ename) as path
    from scott.emp
    group by deptno
    
        DEPTNO PATH
    ---------- --------------------------------------------------------------------------------
     10 CLARK,MILLER,KING
     20 SMITH,FORD,ADAMS,SCOTT,JONES
     30 ALLEN,JAMES,TURNER,BLAKE,MARTIN,WARD
    Priestess©版权所有,禁止转载
  • 相关阅读:
    php设置和获取cookie
    字符截取 支持UTF8/GBK
    PHP自毁程序
    php短信发送
    PHP版QQ互联OAuth示例代码分享
    javascript中window.open()与window.location.href的区别
    SpringBoot文件上传
    IDEA或Webstorm设置Ctrl+滚轮调整字体大小
    IDEA和WebStorm破解教程--激活80年(ideaIU-2018.3.6以及之前的版本)
    3 字节的 UTF-8 序列的字节 2 无效
  • 原文地址:https://www.cnblogs.com/priestess-zhao/p/8178970.html
Copyright © 2020-2023  润新知