免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1643 | 回复: 0
打印 上一主题 下一主题

写了一个聚合函数, 将字符串连接起来 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-01 17:03 |只看该作者 |倒序浏览

  1.       create or replace type connstrImpl as object
  2.       (
  3.         currentstr varchar2(4000),
  4.         currentseprator varchar2(8),
  5.         static function ODCIAggregateInitialize(sctx IN OUT connstrImpl)
  6.           return number,
  7.         member function ODCIAggregateIterate(self IN OUT connstrImpl,
  8.           value IN VARCHAR2) return number,
  9.         member function ODCIAggregateTerminate(self IN connstrImpl,
  10.           returnValue OUT VARCHAR2, flags IN number) return number,
  11.         member function ODCIAggregateMerge(self IN OUT connstrImpl,
  12.           ctx2 IN connstrImpl) return number
  13.       );
  14.       /


  15.       create or replace type body connstrImpl is
  16.       static function ODCIAggregateInitialize(sctx IN OUT connstrImpl)
  17.       return number is
  18.       begin
  19.         sctx := connstrImpl('','/');
  20.         return ODCIConst.Success;
  21.       end;
  22.       member function ODCIAggregateIterate(self IN OUT connstrImpl, value IN VARCHAR2) return number is
  23.       begin
  24.         if self.currentstr is null then
  25.           self.currentstr := value;
  26.         else
  27.           self.currentstr := self.currentstr ||currentseprator || value;
  28.         end if;
  29.         return ODCIConst.Success;
  30.       end;
  31.       member function ODCIAggregateTerminate(self IN connstrImpl, returnValue OUT VARCHAR2, flags IN number) return number is
  32.       begin
  33.         returnValue := self.currentstr;
  34.         return ODCIConst.Success;
  35.       end;
  36.       member function ODCIAggregateMerge(self IN OUT connstrImpl, ctx2 IN connstrImpl) return number is
  37.       begin
  38.         if ctx2.currentstr is null then
  39.           self.currentstr := self.currentstr;
  40.         elsif self.currentstr is null then
  41.           self.currentstr := ctx2.currentstr;
  42.         else
  43.           self.currentstr := self.currentstr || currentseprator || ctx2.currentstr;
  44.         end if;
  45.         return ODCIConst.Success;
  46.       end;
  47.       end;
  48.       /



  49.       CREATE OR REPLACE FUNCTION connstr (input VARCHAR2) RETURN VARCHAR2
  50.       PARALLEL_ENABLE AGGREGATE USING connstrImpl;
  51.       /
复制代码

使用示例如下:

  1. SQL> select connstr(loc) from dept ;

  2. CONNSTR(LOC)
  3. --------------------------------------------------------------------------------
  4. NEW YORK/DALLAS/CHICAGO/BOSTON


复制代码

参考了
Oracle® Data Cartridge Developer's Guide 10g Release 1 (10.1)  User-Defined Aggregate Functions
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP