欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

oracle 数据库单子段多条记录合并一条记录函数

程序员文章站 2022-04-25 21:12:17
...

---- ================================================================ -- 1. 创建oracle TYPE -- ================================================================ CREATE OR REPLACE TYPE COLUMNSTRCAT as object ( str VARCHAR2(4000), static func



---- ================================================================
-- 1. 创建oracle TYPE
-- ================================================================


CREATE OR REPLACE TYPE COLUMNSTRCAT as object
(
str VARCHAR2(4000),
static function ODCIAggregateInitialize(sctx IN OUT ColumnStrCat) return number,
member function ODCIAggregateIterate(self IN OUT ColumnStrCat, value IN VARCHAR2) return number,
member function ODCIAggregateTerminate(self IN ColumnStrCat, returnValue OUT VARCHAR2, flags IN number) return number,
member function ODCIAggregateMerge(self IN OUT ColumnStrCat,ctx2 IN ColumnStrCat) return number
);
-- ================================================================
-- 2. 创建 oracle TYPE BODIES
-- ================================================================
CREATE OR REPLACE TYPE BODY COLUMNSTRCAT is
-- Member procedures and functions
static function ODCIAggregateInitialize(sctx IN OUT ColumnStrCat)
return number
is
begin
sctx := ColumnStrCat('');
return ODCIConst.Success;
end;




member function ODCIAggregateIterate(self IN OUT ColumnStrCat, value IN varchar2) return number is
counter integer;
begin
self.str := self.str||value||',';
return ODCIConst.Success;
end;




member function ODCIAggregateTerminate(self IN ColumnStrCat, returnValue OUT varchar2, flags IN number) return number is
begin
returnValue := substr(self.str,1,length(self.str)-1);
return ODCIConst.Success;
end;




member function ODCIAggregateMerge(self IN OUT ColumnStrCat, ctx2 IN ColumnStrCat) return number is
begin
self.str := self.str||ctx2.str;
return ODCIConst.Success;
end;
end;


-- -- ================================================================
-- 3. 创建 oracle function
-- -- ================================================================
CREATE OR REPLACE FUNCTION COLUMNSTRJOIN (input varchar2) RETURN varchar2

PARALLEL_ENABLE AGGREGATE USING ColumnStrCat;


-- -- ================================================================
-- 4. 使用示例
-- -- ================================================================

select COLUMNSTRJOIN(a.name) from table_name a where a.type > 1