>데이터 베이스 >MySQL 튜토리얼 >oracle 数据库单子段多条记录合并一条记录函数

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

WBOY
WBOY원래의
2016-06-07 15:24:502254검색

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


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.