Explain Oracle user-defined aggregate functions with an instance

Source: Internet
Author: User

(I found it online. I cannot remember the original address. I will record it here and read it later)

 

Oracle Database customization is very powerful. Oracle not only allows users to customize their own functions, but also can customize their own Aggregate functions and analysis functions.

This article will focus on the example of creating a string "sum" using a custom aggregate function:

Code


SQL> CREATE OR REPLACE TYPE T_LINK AS OBJECT (STR VARCHAR2 (30000 ),
Static function odciaggresponinitialize (sctx in out T_LINK) return number,
Member function odciaggregateiterate (self in out T_LINK, value in VARCHAR2) return number,
Member function odciaggresponterminate (self in T_LINK, returnvalue out VARCHAR2, flags in number) return number,
Member function odciaggregatemerge (self in out T_LINK, CTX2 IN T_LINK) RETURN NUMBER
)

/

Type already created.

 

 

Code


SQL> CREATE OR REPLACE TYPE BODY T_LINK IS
Static function odciaggresponinitialize (sctx in out T_LINK) RETURN NUMBER IS
BEGIN
SCTX: = T_LINK (NULL );
Return odciconst. SUCCESS;
END;

Member function odciaggregateiterate (self in out T_LINK, value in VARCHAR2) RETURN NUMBER IS
BEGIN
SELF. STR: = SELF. STR | VALUE;
Return odciconst. SUCCESS;
END;

Member function odciaggresponterminate (self in T_LINK, returnvalue out VARCHAR2, flags in number) RETURN NUMBER IS
BEGIN
RETURNVALUE: = SELF. STR;
Return odciconst. SUCCESS;
END;

Member function odciaggregatemerge (self in out T_LINK, CTX2 IN T_LINK) RETURN NUMBER IS
BEGIN
NULL;
Return odciconst. SUCCESS;
END;

END;

/

The type subject has been created.

 

 


SQL> CREATE OR REPLACE FUNCTION F_LINK (P_STR VARCHAR2) RETURN VARCHAR2 AGGREGATE USING T_LINK;

/

The function has been created.

 

 


SQL> CREATE TABLE TEST (ID NUMBER, NAME VARCHAR2 (20 ));

The table has been created.

 

 

SQL> INSERT INTO TEST VALUES (1, 'aaa ');

One row has been created.

 

 

Code


SQL> INSERT INTO TEST VALUES (2, 'bbb ');

One row has been created.

SQL> INSERT INTO TEST VALUES (1, 'abc ');

One row has been created.

SQL> INSERT INTO TEST VALUES (3, 'ccc ');

One row has been created.

SQL> INSERT INTO TEST VALUES (2, 'ddd ');

One row has been created.

SQL> COMMIT;

Submitted.

SQL> COL NAME FORMAT A60

SQL> SELECT ID, F_LINK (NAME) NAME FROM TEST GROUP BY ID;

ID NAME

----------------------------------------------------------------

1 AAAABC

2 BBBDDD

3 CCC

 

 

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.