Oracle multi-row record merge custom function STRCAT

Source: Internet
Author: User

The problem is as follows:
Www.2cto.com

Actual test conditions: -- create table G_PRODUCT_REL (PRODUCT_ID NUMBER not null, -- Business id PC_ID NUMBER not null -- business category id) SELECT 'insert INTO G_PRODUCT_REL (PRODUCT_ID, PC_ID)
VALUES ('| G. PRODUCT_ID |', '| G. PC_ID |') 'FROM G_PRODUCT_REL G;
The test data is as follows: www.2cto.com

Example 1: select g. PRODUCT_ID, strcat (G. PC_ID) FROM G_PRODUCT
_ Rel g group by g. PRODUCT_ID; -- category of each business www.2cto.com

Example 2: SELECT PC_ID, STRCAT (G. PRODUCT_ID) FROM G_PRODUCT _
Rel g group by g. PC_ID; -- there are services www.2cto.com in each category

The following three SQL statements CREATE this FUNCTION: Using CREATE OR REPLACE FUNCTION strcat (input VARCHAR2)
RETURN VARCHAR2 PARALLEL_ENABLE aggregate using strcat_type; Specify create or replace type strcat_type as object (currentstr varchar2 (4000), currentseprator varchar2 (8), static function compute (sctx in out strcat_type) return number, member function ODCIAggregateIterate (self IN OUT
Strcat_type, value IN VARCHAR2) return number, member function odciaggresponterminate (self IN strcat _
Type, returnValue OUT VARCHAR2, flags IN number) return number, member function ODCIAggregateMerge (self in out strcat_type,
Ctx2 IN strcat_type) return number) specify create or replace type body strcat_type is static function odciaggresponinitialize (sctx in out strcat_type) return number is begin sctx: = strcat_type ('',', '); return ODCIConst. success; end; member function ODCIAggregateIterate (self in out strcat_type,
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 odciaggresponterminate (self IN strcat_type,
ReturnValue OUT VARCHAR2, flags IN number) return number is begin returnValue: = self. currentstr; return ODCIConst. Success; end; member function ODCIAggregateMerge (self in out strcat_type,
Ctx2 IN strcat_type) 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; the above functions are written by our DBA, but now more than 10 Gb of database has been added
The wm_concat (column) function merges fields. Haha

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.