当前位置:Gxlcms > mysql > oracle数据库单子段多条记录合并一条记录函数

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

时间:2021-07-01 10:21:17 帮助过:134人阅读

---- ================================================================ -- 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

人气教程排行