自定义聚集函数<br><br>SQL> CREATE OR REPLACE TYPE T_LINK AS OBJECT (<br> 2 STR VARCHAR2(4000),<br> 3 STATIC FUNCTION ODCIAGGREGATEINITIALIZE(SCTX IN OUT T_LINK) RETURN NUMBER,<br> 4 MEMBER FUNCTION ODCIAGGREGATEITERATE(SELF IN OUT T_LINK, VALUE IN VARCHAR2) RETURN NUMBER,<br> 5 MEMBER FUNCTION ODCIAGGREGATETERMINATE(SELF IN T_LINK, RETURNVALUE OUT VARCHAR2, FLAGS IN NUMBER) RETURN NUMBER,<br> 6 MEMBER FUNCTION ODCIAGGREGATEMERGE(SELF IN OUT T_LINK, CTX2 IN T_LINK) RETURN NUMBER )<br> 7 /<br><br>Type created<br><br>SQL> <br>SQL> CREATE OR REPLACE TYPE BODY T_LINK IS<br> 2 STATIC FUNCTION ODCIAGGREGATEINITIALIZE(SCTX IN OUT T_LINK) RETURN NUMBER IS<br> 3 BEGIN<br> 4 SCTX := T_LINK(NULL);<br> 5 RETURN ODCICONST.SUCCESS;<br> 6 END;<br> 7 MEMBER FUNCTION ODCIAGGREGATEITERATE(SELF IN OUT T_LINK, VALUE IN VARCHAR2) RETURN NUMBER IS<br> 8 BEGIN<br> 9 IF SELF.STR IS NULL THEN<br> 10 SELF.STR:=VALUE;<br> 11 ELSE<br> 12 SELF.STR := SELF.STR || ';' || VALUE;<br> 13 END IF;<br> 14 RETURN ODCICONST.SUCCESS;<br> 15 END;<br> 16 MEMBER FUNCTION ODCIAGGREGATETERMINATE(SELF IN T_LINK, RETURNVALUE OUT VARCHAR2, FLAGS IN NUMBER) RETURN NUMBER IS<br> 17 BEGIN<br> 18 RETURNVALUE := SELF.STR;<br> 19 RETURN ODCICONST.SUCCESS;<br> 20 END;<br> 21 MEMBER FUNCTION ODCIAGGREGATEMERGE(SELF IN OUT T_LINK, CTX2 IN T_LINK) RETURN NUMBER IS<br> 22 BEGIN<br> 23 NULL;<br> 24 RETURN ODCICONST.SUCCESS;<br> 25 END;<br> 26 END;<br> 27 /<br><br>Type body created<br><br>SQL> <br>SQL> CREATE OR REPLACE FUNCTION F_LINK(P_STR VARCHAR2) RETURN VARCHAR2<br> 2 AGGREGATE USING T_LINK;<br> 3 /<br><br>Function created<br><br>SQL> <br>SQL> INSERT INTO test VALUES('A','A1','A2');<br><br>1 row inserted<br><br>SQL> INSERT INTO test VALUES('A','A3','A4');<br><br>1 row inserted<br><br>SQL> INSERT INTO test VALUES('B','B1','B2');<br><br>1 row inserted<br><br>SQL> INSERT INTO test VALUES('B','B3','B4');<br><br>1 row inserted<br><br>SQL> SELECT col1,f_link(col2),f_link(col3) FROM test GROUP BY col1;<br><br>COL1 F_LINK(COL2) F_LINK(COL3)<br>---------- ------------ ---------------------<br>A A1;A3 A2;A4<br>B B1;B3 B2;B4