If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Nested XML selects

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-22-06, 05:15
iblair iblair is offline
Registered User
 
Join Date: Sep 2005
Posts: 16
Nested XML selects

I am trying to used a stored proc to return an xml string from sysibm.sqltables formatted as follows:

<TABLES>
<schema name='schema1'>
<table>table1</table>
<table>table2</table>
</schema>
<schema name='schema2'>
<table>table3</table>
<table>table4></table>
</schema>
</TABLES>

The closest I can get is as follows:

SELECT XML2CLOB (XMLELEMENT (NAME "TABLES", XMLELEMENT (NAME "schema", XMLATTRIBUTES (table_schem AS "name"),
XMLAGG (XMLFOREST(table_name AS "table")))))
FROM sysibm.sqltables GROUP BY table_schem;

This produces a separate result for each schema because of the GROUP BY statement, but if this is left out an error occurs saying that table_schem needs to be in a GROUP BY statement.

Does anyone have any ideas?

Many thanks,
Ian
Reply With Quote
  #2 (permalink)  
Old 06-26-06, 03:31
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
Why would you want to use GROUP BY, you don't use any aggerate function. I think ORDER BY is appropriate.
Code:
SELECT XML2CLOB (
  XMLELEMENT (NAME "TABLES", 
   XMLELEMENT (NAME "schema", 
   XMLATTRIBUTES (table_schem AS "name"),
   XMLAGG (XMLFOREST(table_name AS "table"))
   )
  )
)
FROM sysibm.sqltables
ORDER BY table_schem, table_name;
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
  #3 (permalink)  
Old 06-26-06, 05:57
iblair iblair is offline
Registered User
 
Join Date: Sep 2005
Posts: 16
That won't work :o)

I am using the group by statement, because using the alternative suggested give the error message below:

DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0119N An expression starting with "TABLE_SCHEM" specified in a SELECT
clause, HAVING clause, or ORDER BY clause is not specified in the GROUP BY
clause or it is in a SELECT clause, HAVING clause, or ORDER BY clause with a
column function and no GROUP BY clause is specified. LINE NUMBER=5.
SQLSTATE=42803

Thanks,
Ian
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On