concatenating strings from two different rows in a table
We are attempting to rework the SQL in a product. The product stores XML in a table as follows:
XML_STORAGE - UID IDENTITY - PARENT_ID INTEGER - SEQ INTEGER - XML VARCHAR(3800)
The current way of doing this is as follows:
Retrieve all ROWS for PARENT_ID = n. Then go over the fetched rows in the code and concatenate the XML strings into one large XML before parsing. The SEQ column is used to ORDER the result so the XML strings can be concatenated properly. Hopefully that is clear.
What we are attempting to do is rework this so we can use a SQL variant to retrieve the whole string and just fetch one row back from DB2. Is there a DB2 function that will allow us to concatenate the string in all of these rows into one large string in the resultset. How would 开发者_StackOverflowsuch a SQL look. Please let me know. Any help is much appreciated.
Thanks! - Azeem
Check recursive queries in DB2, such as mentioned here
Yes there is. Lock up XMLELEMENT
, XMLAGG
, and XMLSERIALIZE
. That should do the trick for most people. An example you can see in the following question Output a DB2 SQL XML query to a unique XML file
Edit:
Why don't you do something like select '<tag1>', col1. '</tag1><tag2>', col2, '</tag2>' from table
? If you need to combine all rows into one than run a postprocessing to remove the line breaks.
精彩评论