如果每行的结构相同,则可以设置模板。例如:
SQL> create table source
2 (
3 id number,
4 cola varchar2(20),
5 colb number
6 );
Table created.
SQL>
SQL> insert into source values (1, 'teststr', 23898.234);
1 row created.
SQL> insert into source values (2, '', -9989.00);
1 row created.
SQL> insert into source values (3, 'test again', 0);
1 row created.
SQL> commit;
Commit complete.
SQL>
SQL>
SQL> with xml_template as (select xmltype(
2 '<root>
3 <somefield>asdas</somefield>
4 <somefield2>asdas</somefield2>
5 <typ>
6 <col1>.</col1>
7 </typ>
8 <col2>.</col2>
9 <col3>.</col3>
10 </root>') xml
11 from dual)
12 select updatexml(
13 xt.xml,
14 '/root/typ/col1/text()', s.id,
15 '/root/col2/text()', s.cola,
16 '/root/col3/text()', s.colb)
17 from xml_template xt
18 cross join source s;
UPDATEXML(XT.XML,'/ROOT/TYP/COL1/TEXT()',S.ID,'/ROOT/COL2/TEXT()',S.COLA,'/ROOT/
--------------------------------------------------------------------------------
<root>
<somefield>asdas</somefield>
<somefield2>asdas</somefield2>
<typ>
<col1>1</col1>
</typ>
<col2>teststr</col2>
<col3>23898.234</col3>
</root>
<root>
<somefield>asdas</somefield>
<somefield2>asdas</somefield2>
<typ>
<col1>2</col1>
</typ>
<col2/>
<col3>-9989</col3>
</root>
<root>
<somefield>asdas</somefield>
<somefield2>asdas</somefield2>
<typ>
<col1>3</col1>
</typ>
<col2>test again</col2>
<col3>0</col3>
</root>
因此,updatexml
只需填写每行不同的字段。