Hi all!
The purpose is to get concatenated string from varchar field.
There're 2 ways - use combination of XML2CLOB(XMLAGG(XMLELEMENT(?)))
or use cursor with looping throgh each sql query row. (or may be there's some db2 built-in function)
Which way will get more performance?
Here's sample code?
The purpose is to get concatenated string from varchar field.
There're 2 ways - use combination of XML2CLOB(XMLAGG(XMLELEMENT(?)))
or use cursor with looping throgh each sql query row. (or may be there's some db2 built-in function)
Which way will get more performance?
Here's sample code?
Code:
CREATE FUNCTION DLIB.Concat_Subjects(pDocid CHAR(16) FOR BIT
DATA, IN pLen INT)
--
RETURNS VARGRAPHIC(500)
BEGIN
DECLARE len int;--
DECLARE subjects VARGRAPHIC(200);--
SET subjects = (SELECT REPLACE(REPLACE(XML2CLOB(XMLAGG(XMLELEMENT(NAME a, s.name))),'<A>',' '),'</A>',',')
FROM DLIB.dl_docsubjects ds INNER JOIN DLIB.dl_subjects s ON s.id = ds.subjectid WHERE ds.documentid = pDocid);--
SET len = LENGTH(subjects);--
-- remove start space & ending comma
SET subjects = SUBSTRING(subjects, 2, LEN - 2, CODEUNITS16);--
IF (pLen > 0) AND (len > pLen) THEN
SET subjects = SUBSTRING(subjects, 1, pLen, CODEUNITS16)||'...';--
END IF;--
RETURN subjects;--
END;