Multi-line merge aggregate functions in Oracle

Source: Internet
Author: User

In an Oracle database, you can use the Wm_concat (column) function for field merging, but this method is not recommended by Oracle because the Wmsys user is used by the workspace Manager, whose function objects may differ from version to exception. This change is reflected in 11.2.0.3 and 10.2.0.5. Originally the WM_CONCAT function returned a value of VARCHAR2, but in some versions it becomes more CLOB. This change has resulted in many exceptions to the program. Instead of using this intrinsic function of workspace, Oracle recommends that users use custom functions to implement this functionality. This function contains a type, type Body, and function, which can be implemented by reference to the implementation of Oracle.

country              city
----------------------------------------
China                  Taipei
China          & nbsp;      Hong Kong
China                  Shanghai
Japan             & nbsp;   Tokyo
Japan                  Osaka

Country City
-------------------- --------------------
China Taipei Hong Kong Shanghai
Osaka, Tokyo, Japan

Select Country,strcat from T_city GROUP by country

1. Create a Type

Create or replace type Strcat_type as Object (
cat_string varchar2 (4000),
static function Odciaggregateinitialize (Cs_ctx in Out Strcat_type) return number,
member function odciaggregateiterate (self on Out strcat_type,value in VARCHAR2) return
Number
member function Odciaggregatemerge (self in Out strcat_type,ctx2 on out Strcat_type)
return number,
member function odciaggregateterminate (self on out strcat_type,returnvalue out
Varchar2,flags in number) return number
);

2. Create a type body

create or replace 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

3. Create a function
CREATE OR REPLACE FUNCTION strcat (input varchar2)
RETURN VARCHAR2
Parallel_enable AGGREGATE USING Strcat_type;

Multi-line merge aggregate functions in Oracle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.