Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4391350
  • 博文数量: 109
  • 博客积分: 10011
  • 博客等级: 上将
  • 技术积分: 2457
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-18 19:04
文章分类

全部博文(109)

文章存档

2011年(1)

2010年(10)

2009年(36)

2008年(62)

我的朋友

分类: Oracle

2008-03-11 18:32:02

写了一个聚合函数, 将字符串连接起来

[code]
      create or replace type connstrImpl as object
      (
        currentstr varchar2(4000),
        currentseprator varchar2(8),
        static function ODCIAggregateInitialize(sctx IN OUT connstrImpl)
          return number,
        member function ODCIAggregateIterate(self IN OUT connstrImpl,
          value IN VARCHAR2) return number,
        member function ODCIAggregateTerminate(self IN connstrImpl,
          returnValue OUT VARCHAR2, flags IN number) return number,
        member function ODCIAggregateMerge(self IN OUT connstrImpl,
          ctx2 IN connstrImpl) return number
      );
      /


      create or replace type body connstrImpl is
      static function ODCIAggregateInitialize(sctx IN OUT connstrImpl)
      return number is
      begin
        sctx := connstrImpl('','/');
        return ODCIConst.Success;
      end;
      member function ODCIAggregateIterate(self IN OUT connstrImpl, value IN VARCHAR2) return number is
      begin
        if self.currentstr is null then
          self.currentstr := value;
        else
          self.currentstr := self.currentstr ||currentseprator || value;
        end if;
        return ODCIConst.Success;
      end;
      member function ODCIAggregateTerminate(self IN connstrImpl, returnValue OUT VARCHAR2, flags IN number) return number is
      begin
        returnValue := self.currentstr;
        return ODCIConst.Success;
      end;
      member function ODCIAggregateMerge(self IN OUT connstrImpl, ctx2 IN connstrImpl) return number is
      begin
        if ctx2.currentstr is null then
          self.currentstr := self.currentstr;
        elsif self.currentstr is null then
          self.currentstr := ctx2.currentstr;
        else
          self.currentstr := self.currentstr || currentseprator || ctx2.currentstr;
        end if;
        return ODCIConst.Success;
      end;
      end;
      /



      CREATE OR REPLACE FUNCTION connstr (input VARCHAR2) RETURN VARCHAR2
      PARALLEL_ENABLE AGGREGATE USING connstrImpl;
      /
[/code]
使用示例如下:
[code]
SQL> select connstr(loc) from dept ;

CONNSTR(LOC)
--------------------------------------------------------------------------------
NEW YORK/DALLAS/CHICAGO/BOSTON
阅读(1436) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~