dbms_xmlgen oracle获取动态xmltype

时间:2017-06-11 09:19:51

标签: sql xml oracle dbms-xmlgen

我在oracle中有一个表格:

select op_id, dimorder, title from t:

OP_ID|DIMORDER|TITLE
-----+--------+-----
  312|       1|AAA
  312|       2|BBB

我想用dbms_xmlgen.getXml来实现这个结果(只有一行):

OP_ID|NEW_COLUMN
-----+----------
  312|<ROWSET><ROW><DIMORDER>1</DIMORDER><TITLE>AAA</TITLE>
       </ROW><ROW><DIMORDER>2</DIMORDER><TITLE>BBB</TITLE></ROW></ROWSET>

非常感谢您的帮助

1 个答案:

答案 0 :(得分:1)

SQL / XML生成函数是从关系数据生成XML的首选方法。假设你有多组op_id行就像这样工作..

 SQL>  set long 10000
 SQL> with MY_TABLE as
   2  (
   3     select 312 as op_id, 1 as dimorder, 'AAA' as title
   4        from dual
   5     union all
   6     select 312 as op_id, 2 as dimorder, 'BBB' as title
   7        from dual
   8     union all
   9     select 313 as op_id, 1 as dimorder, 'CCC' as title
  10        from dual
  11     union all
  12     select 313 as op_id, 2 as dimorder, 'DDD' as title
  13        from dual
  14  )
  15  select XMLELEMENT("ROWSET",
  16           XMLELEMENT("ROW",
  17             XMLAGG(
  18               XMLFOREST("DIMORDER" as DIMORDER, "TITLE" as TITLE)
  19               ORDER BY DIMORDER
  20             )
  21           )
  22         ) RESULT
  23    from MY_TABLE
  24   group by OP_ID
  25  /

 RESULT
 --------------------------------------------------------------------------------
 <ROWSET><ROW><DIMORDER>1</DIMORDER><TITLE>AAA</TITLE><DIMORDER>2</DIMORDER><TITLE>BBB</TITLE></ROW></ROWSET>

 <ROWSET><ROW><DIMORDER>1</DIMORDER><TITLE>CCC</TITLE><DIMORDER>2</DIMORDER><TITLE>DDD</TITLE></ROW></ROWSET>


 SQL>