在包中使用用户定义的聚合函数

时间:2012-04-12 12:27:30

标签: oracle package aggregate-functions

我的oracle版本是10.2。 我有一个user_defined聚合函数,它使用如下对象:

create type strcat_type as object ( 
cat_string varchar2(4000), 
static function ODCIAggregateInitialize(cs_ctx In Out strcat_type) return number, 
member function ODCIAggregateIterate(self In Out strcat_type,value in varchar2) return           
number, 
member function ODCIAggregateMerge(self In Out strcat_type,ctx2 In Out strcat_type)       
return number, 
member function ODCIAggregateTerminate(self In Out strcat_type,returnValue Out   
varchar2,flags in number) return number 
)

但是当我尝试将此对象放入包中时,

create or replace package common is
type strcat_type as object ( 
cat_string varchar2(4000), 
static function ODCIAggregateInitialize(cs_ctx In Out strcat_type) return number, 
member function ODCIAggregateIterate(self In Out strcat_type,value in varchar2) return           
number, 
member function ODCIAggregateMerge(self In Out strcat_type,ctx2 In Out strcat_type)       
return number, 
member function ODCIAggregateTerminate(self In Out strcat_type,returnValue Out   
varchar2,flags in number) return number 
)
end common;

它会导致pls-00540,pls-00707,如何使用这个对象创建一个包?如何将我的类型主体和所有那些静态和成员函数放在包体中?

我这样的体型:

create type body strcat_type is 
static function ODCIAggregateInitialize(cs_ctx IN OUT strcat_type) return number 
is 
begin 
  cs_ctx := strcat_type( null ); 
  return ODCIConst.Success; 
end; 

member function ODCIAggregateIterate(self IN OUT strcat_type, 
                                   value IN varchar2 ) 
return number 
is 
begin 
   self.cat_string := self.cat_string || ','|| value; 
   return ODCIConst.Success; 
end; 

member function ODCIAggregateTerminate(self IN Out strcat_type, 
                                     returnValue OUT varchar2, 
                                     flags IN number) 
return number 
is 
begin 
  returnValue := ltrim(rtrim(self.cat_string,','),','); 
  return ODCIConst.Success; 
end; 

member function ODCIAggregateMerge(self IN OUT strcat_type, 
                                 ctx2 IN Out strcat_type) 
return number 
is 
begin 
  self.cat_string := self.cat_string || ',' || ctx2.cat_string; 
  return ODCIConst.Success; 
end;
end;

我的功能是这样的:

CREATE or replace 
FUNCTION strcat(input varchar2 ) 
RETURN varchar2 
PARALLEL_ENABLE AGGREGATE USING strcat_type; 

如何将所有这些打包成一个包,当我需要聚合时,我只是把'common.strcat(colName)'?

1 个答案:

答案 0 :(得分:1)

OBJECT类型是 SQL 对象(如表,索引......),它们不能在包中定义(就像你不会在包中定义索引或视图一样) )。

参见相关的SO问题:Possible to create Oracle Database object types inside of PL/SQL?