Oracle中listagg的替代方案?

时间:2012-02-23 11:51:56

标签: sql oracle varchar clob

listagg是Oracle 11.2中引入的一个函数!现在这个函数正在困扰我们分配,我们正在从MySQL迁移到Oracle,我们有这个查询:

SELECT
    p_id,
    MAX(registered) AS registered,
    listagg(MESSAGE, ' ') within GROUP (ORDER BY registered) AS MESSAGE
  FROM
    umm_parent_id_remarks_v m
  GROUP BY
    m.p_id;
据我们所知,

在MySQL中运行良好 什么烦恼我们在Oracle下它返回VARCAR而不是我们需要的CLOB! 文字很大,我们确实需要CLOB

这是我试图做的事情!

创建一个CLOB类型的CLOB_T表!

然后创建函数

create or replace
function listaggclob (t in clob_t) 
  return clob
as 
  ret clob := '';
  i   number;
begin
  i := t.first;
  while i is not null loop
    if ret is not null then
      ret := ret || ' ';
    end if;
    ret := ret || t(i);
    i := t.next(i);
  end loop;
  return ret;
end;

现在如果我运行它:

  SELECT
        p_id,
        MAX(registered) AS registered,
        listaggclob(cast(collect (MESSAGE) as clob_t)) MESSAGE
      FROM
        umm_parent_id_remarks_v m
      GROUP BY
        m.p_id;

我得到了

ORA-22814:属性或元素值大于类型

中指定的值

有什么解决方案吗?

谢谢你

6 个答案:

答案 0 :(得分:17)

答案 1 :(得分:3)

WM_CONCAT为我工作。

SELECT replace(WMSYS.WM_CONCAT(myTable.name), ',', ';')
FROM myTable
GROUP BY myTable.id

我用“替换”包装它以指定与WM_CONCAT(',')使用的分隔符不同的项目分隔符(';')。

答案 2 :(得分:2)

您可能需要查看user-defined aggregate functions

显示了不同的字符串聚合技术here。它们包含用户定义的聚合函数的示例。

答案 3 :(得分:2)

您可以使用ORA-22814代替MULTISET来解决COLLECT错误:

SELECT
    p_id,
    MAX(registered) AS registered,
    listaggclob(cast(multiset(
        select MESSAGE
        from umm_parent_id_remarks_v
        where umm_parent_id_remarks_v.p_id = m.p_id
    ) as clob_t)) MESSAGE
  FROM
    umm_parent_id_remarks_v m
  GROUP BY
    m.p_id;

答案 4 :(得分:2)

使用xmlAgg,示例如下所示:

SELECT RTRIM(XMLAGG(XMLELEMENT(E,colname,',').EXTRACT('//text()') ORDER BY colname).GetClobVal(),',') AS LIST
FROM tablename;

这将返回clob值,因此无需创建自定义函数。

答案 5 :(得分:0)

- 创建Clobe类型 - 创建或替换类型“MSCONCATIMPL_CLOB”作为对象(     结果字符串CLOB,     分隔符VARCHAR2(10),

STATIC FUNCTION odciaggregateinitialize ( io_srccontext IN OUT msconcatimpl_clob ) RETURN NUMBER,

MEMBER FUNCTION odciaggregateiterate (
    self IN OUT msconcatimpl_clob,
    value   IN CLOB
) RETURN NUMBER,

MEMBER FUNCTION odciaggregateterminate (
    self            IN msconcatimpl_clob,
    o_returnvalue   OUT CLOB,
    i_flags         IN NUMBER
) RETURN NUMBER,

MEMBER FUNCTION odciaggregatemerge (
    self IN OUT msconcatimpl_clob,
    i_ctx2   IN msconcatimpl_clob
) RETURN NUMBER

); / - 创建Clobe Type Body -

创建或替换类型身体“MSCONCATIMPL_CLOB”是     静态功能odciaggregateinitialize(io_srccontext IN OUT msconcatimpl_clob)返回编号         IS     开始         io_srccontext:= msconcatimpl_clob(             空值,             空值         );         io_srccontext.delimiter:='';         返回odciconst.success;     END odciaggregateinitialize;

MEMBER FUNCTION odciaggregateiterate (
    self IN OUT msconcatimpl_clob,
    value   IN CLOB
) RETURN NUMBER
    IS
BEGIN
    IF
        value IS NOT NULL
    THEN
        IF
            self.resultstring IS NULL
        THEN
            self.resultstring := self.resultstring || value;
        ELSE
            self.resultstring := self.resultstring
             || self.delimiter
             || value;
        END IF;
    END IF;

    RETURN odciconst.success;
END odciaggregateiterate;

MEMBER FUNCTION odciaggregateterminate (
    self            IN msconcatimpl_clob,
    o_returnvalue   OUT CLOB,
    i_flags         IN NUMBER
) RETURN NUMBER
    IS
BEGIN
    o_returnvalue := self.resultstring;
    RETURN odciconst.success;
END odciaggregateterminate;

MEMBER FUNCTION odciaggregatemerge (
    self IN OUT msconcatimpl_clob,
    i_ctx2   IN msconcatimpl_clob
) RETURN NUMBER
    IS
BEGIN
    IF
            self.resultstring IS NULL
        AND
            i_ctx2.resultstring IS NOT NULL
    THEN
        self.resultstring := i_ctx2.resultstring;
    ELSIF
        self.resultstring IS NOT NULL
    AND
        i_ctx2.resultstring IS NOT NULL
    THEN
        self.resultstring := self.resultstring
         || self.delimiter
         || i_ctx2.resultstring;
    END IF;

    RETURN odciconst.success;
END odciaggregatemerge;

END; /

- 创建Clobe函数 -

创建或替换函数ms_concat_clob(输入VARCHAR2)RETURN CLOB     PARALLEL_ENABLE 使用msconcatimpl_clob汇总; /