Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2016-06-07 15:24:50
Original
2224 people have browsed it

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


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template