Comments on: Creating an XMLType based on Object Types Friends of Oracle and Java Wed, 24 Jun 2015 09:59:44 +0000 hourly 1 By: tokke Wed, 08 Feb 2012 15:13:02 +0000 alex, great blog, as usual. thanks. one question remains. i can’t appreciate the last definition of the type called all_departments_ot.
in the query without the xmltype you don’t use it. in the one that includes the xmltype you do. can you explain that difference, and the need for the use of the all_departments_ot?
much obliged,

By: Marco Gralike Thu, 23 Dec 2010 18:59:19 +0000 Oh, I agree, just pointed out that in this case it might also be solved via a simple select within XMLTYPE(CURSOR())

By: Alex Nuijten Thu, 23 Dec 2010 15:43:30 +0000 @Marco,

Personally I find the Object Types (with the CASTs and COLLECTs) a lot easier to read (and write) than using XMLType (with all the XMLElement, XMLAgg and so on).

Thank you for your comment.

By: Marco Gralike Thu, 23 Dec 2010 15:19:02 +0000 The problem with object types is that complex nested structures will have very fast the same issue as your first XML/SQL example.

Based on your needs the easiest way to achieve this would be to use your query as input for XMLTYPE which can handle CURSOR constructs to for example:

select XMLTYPE(CURSOR( select d.department_name, d.manager_id, e.first_name, e.last_name, e.manager_id from departments d join employees   e on d.department_id = e.department_id where d.department_id = 20)) FROM DUAL;

Be aware that the “pretty print” format is not needed and creates overhead for the XML parser. Use XMLSerialize to bring it back to one big string without all the CR/LF and whitespace, if needed (this is the default in Oracle 11g and onwards)