tags:

views:

392

answers:

2

Hello,

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 such a SQL look. Please let me know. Any help is much appreciated.

Thanks! - Azeem

A: 

Check recursive queries in DB2, such as mentioned here

Unreason
Goran, thanks for the info. I'll check that out now.
Azeem
Goran, unfortunately, I can't use common table expressions or recursive queries because of the requirement that we support DB2 z.OS v8 CM (which is effective DB2 v7). It doesn't look like DB2 v7 supports the common-table expressions syntax. :(
Azeem
don't have that version to test it out, but v7 SQL reference ftp://ftp.software.ibm.com/ps/products/db2/info/vr7/pdf/letter/db2s0e70.pdf says you can. example on pdf's page 1341 (ah, the 4-digit page numbers of IBM manuals :) )
Unreason
Goran, thank you for pointing that out. I didn't realize that v7 supported it. I'll give it a shot. Thanks again!
Azeem
A: 

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 http://stackoverflow.com/questions/2350588/output-a-db2-sql-xml-query-to-a-unique-xml-file/2359724#2359724

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.

Peter Schuetze
Thanks for the info Peter. Unfortunately, given that this is DB2 v7, we do not have the luxury of using XML specific DB2 syntax. :(
Azeem