oracle 定义聚合函数,Oracle 自定义聚合函数

本文介绍了如何使用ODCIAggregate在Oracle中创建或替换str_concat_type类型,该类型提供了初始化、迭代、合并和终止函数,功能类似于wm_concat。作者通过示例展示了如何实现在PL/SQL中处理多个字符串的聚合操作。
摘要由CSDN通过智能技术生成

create or replace type str_concat_type as object (

cat_string varchar2(4000),

static function ODCIAggregateInitialize(cs_ctx In Out str_concat_type) return number,

member function ODCIAggregateIterate(self In Out str_concat_type,value in varchar2) return number,

member function ODCIAggregateMerge(self In Out str_concat_type,ctx2 In Out str_concat_type) return number,

member function ODCIAggregateTerminate(self In Out str_concat_type,returnValue Out varchar2,flags in number) return number

)

/

create or replace type body str_concat_type is

static function ODCIAggregateInitialize(cs_ctx IN OUT str_concat_type) return number

is

begin

cs_ctx := str_concat_type( null );

return ODCIConst.Success;

end;

member function ODCIAggregateIterate(self IN OUT str_concat_type,

value IN varchar2 )

return number

is

begin

--1. concat string

self.cat_string := self.cat_string || ‘,‘|| value;

-- 2.get union set

-- if instr(self.cat_string, value ) = 0 or self.cat_string is null then

-- self.cat_string := self.cat_string || ‘,‘ || value ;

-- else

-- self.cat_string := self.cat_string ||‘‘ ;

-- end if ;

return ODCIConst.Success;

end;

member function ODCIAggregateTerminate(self IN Out str_concat_type,

returnValue OUT varchar2,

flags IN number)

return number

is

begin

returnValue := ltrim(rtrim(self.cat_string,‘,‘),‘,‘);

return ODCIConst.Success;

end;

member function ODCIAggregateMerge(self IN OUT str_concat_type,

ctx2 IN Out str_concat_type)

return number

is

begin

self.cat_string := self.cat_string || ‘,‘ || ctx2.cat_string;

return ODCIConst.Success;

end;

end;

/

CREATE OR REPLACE FUNCTION str_concat(input varchar2) RETURN varchar2

PARALLEL_ENABLE

AGGREGATE USING str_concat_type;

/

234d6cf35e5a8ff885a597066b7c49fa.png

实现的功能和wm_concat一样。

原文:https://www.cnblogs.com/ayumie/p/10002486.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值