Friday, December 11, 2009

SQL output using XML functions

I am not sure how XML function works in Oracle but it indeed gives ouptput in a much formatted way which we need most of the times.

Consider a table with following records
col1 col2
1 A
2 B
3 C
1 D
2 S

Execute this query
select col1,
rtrim (xmlagg (xmlelement (e, col2 ',')).extract ('//text()'), ',') col2 from tbl1
group by col1
the output will be
col1 col2
1 A,D
2 B,S
3 C

Most of the times we need similar output as shown above which we will do by writing long PL/SQL codes by there is a much easier way to achieve this by using Oracle XML functions. Explore your ideas in XML / Oracle functions