oracle中,行转列函数wm_concat()结果有长度限制,重写该函数解决

 

--Type
CREATE OR REPLACE TYPE zh_concat_im AUTHID CURRENT_USER AS OBJECT
(
  CURR_STR clob,
  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 clob,
                                         FLAGS       IN NUMBER)
    RETURN NUMBER,
  MEMBER FUNCTION ODCIAGGREGATEMERGE(SELF  IN OUT zh_concat_im,
                                     SCTX2 IN zh_concat_im) RETURN NUMBER
);

--TYPE BODY
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 clob,
                                         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; 

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

首先创建一个Type,一个Type Body,最后利用该Type和Type Body 创建一个返回类型为Clob的function,返回的结果是中间用逗号隔开的clob字段,如下图所示:

select zh_concat(distinct(t.standardsort)) standardsort from TB_SD_STANDARD_CHOOSE_ADVISE t

结果如下:

在后台操作获取该clob字段的内容如以下代码所示:

BufferedReader in = null;
                            StringWriter out = null;
                            String content = "";
                            try {
                                Clob clob = (Clob)objs[5];
                                in = new BufferedReader(clob.getCharacterStream());
                                out = new StringWriter();
                                int c;
                                while ((c = in.read()) != -1) {
                                    out.write(c);
                                }
                                content = out.toString();
                            } catch (Exception e) {
                                e.printStackTrace();
                            } finally {
                                in.close();
                                out.close();
                            }

 

转载于:https://www.cnblogs.com/lowerCaseK/p/wm_concat_limit.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值